麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 網站 > WEB開發 > 正文

C# __ WebService上傳文件

2024-04-27 15:16:58
字體:
來源:轉載
供稿:網友

一. 項目截圖

這里寫圖片描述

二. Webapplication2 __code

新建一個Webservice1.asmx文件using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Security.Cryptography;using System.Web;using System.Web.Services;namespace WebApplication2{ /// <summary> /// WebService1 的摘要說明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = Wsiprofiles.BasicPRofile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允許使用 asp.net Ajax 從腳本中調用此 Web 服務,請取消注釋以下行。 // [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public bool CreateFile(string fileName) { bool isCreate = true; try { //首先設置上傳服務器文件的路徑 然后發布web服務 發布的時候要自己建一個自己知道的文件夾 "C:/NMGIS_Video/" "C:/NMGIS_Video/" fileName = Path.Combine(Server.MapPath("") + @"/Video/" + Path.GetFileName(fileName)); FileStream fs = new FileStream(fileName, FileMode.Create, Fileaccess.ReadWrite, FileShare.ReadWrite); fs.Close(); } catch { isCreate = false; } return isCreate; } [WebMethod] public bool Append(string fileName, byte[] buffer) { bool isAppend = true; try { // fileName = Path.Combine(@"D:/ServiceEx/" + Path.GetFileName(fileName)); fileName = Path.Combine(Server.MapPath("") + @"/Video/" + Path.GetFileName(fileName)); FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); fs.Seek(0, SeekOrigin.End); fs.Write(buffer, 0, buffer.Length); fs.Close(); } catch { isAppend = false; } return isAppend; } [WebMethod] public bool Verify(string fileName, string md5) { bool isVerify = true; try { fileName = Path.Combine(Server.MapPath("") + @"/Video/" + Path.GetFileName(fileName)); FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); MD5CryptoServiceProvider p = new MD5CryptoServiceProvider(); byte[] md5buffer = p.ComputeHash(fs); fs.Close(); string md5Str = ""; List<string> strList = new List<string>(); for (int i = 0; i < md5buffer.Length; i++) { md5Str += md5buffer[i].ToString("x2"); } if (md5 != md5Str) isVerify = false; } catch { isVerify = false; } return isVerify; } }}

三. WindowsFormsApplication1 __code

1. 添加 from

這里寫圖片描述

2. 添加服務引用

這里寫圖片描述

3.from1__codeusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Security.Cryptography;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { OpenFileDialog openDialog = new OpenFileDialog(); openDialog.Filter = "地圖數據文件(*.udb,*.udd)|*.udb;*.udd;"; if(openDialog.ShowDialog()==DialogResult.OK) { textBox1.Text = openDialog.FileName; } } private void button2_Click(object sender, EventArgs e) { ////ServiceReference1.WebService1 client = new ServiceReference1.WebService1(); ServiceReference1.WebService1SoapClient client = new ServiceReference1.WebService1SoapClient(); //上傳服務器后的文件名 一般不修改文件名稱 int start = textBox1.Text.LastIndexOf("http://"); int length = textBox1.Text.Length; //string serverfile = textBox1.Text.Substring(start + 1, length - textBox1.Text.LastIndexOf(".")) // + DateTime.Now.ToString("-yyyy-mm-dd-hh-mm-ss") // + textBox1.Text.Substring(textBox1.Text.LastIndexOf("."), textBox1.Text.Length - textBox1.Text.LastIndexOf(".")); string serverfile = "7e168da43b484affa5cbf9cce4832a93" + textBox1.Text.Substring(textBox1.Text.LastIndexOf("."), textBox1.Text.Length - textBox1.Text.LastIndexOf(".")); ; client.CreateFile(serverfile); //要上傳文件的路徑 string sourceFile = textBox1.Text; string md5 = GetMD5(sourceFile); FileStream fs = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); int size = (int)fs.Length; int bufferSize = 1024 * 512; int count = (int)Math.Ceiling((double)size / (double)bufferSize); for (int i = 0; i < count; i++) { int readSize = bufferSize; if (i == count - 1) readSize = size - bufferSize * i; byte[] buffer = new byte[readSize]; fs.Read(buffer, 0, readSize); client.Append(serverfile, buffer); } bool isVerify = client.Verify(serverfile, md5); if (isVerify) MessageBox.Show("上傳成功"); else MessageBox.Show("上傳失敗"); } private string GetMD5(string fileName) { string md5Str = ""; FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); MD5CryptoServiceProvider p = new MD5CryptoServiceProvider(); byte[] md5buffer = p.ComputeHash(fs); fs.Close(); // List<string> strList = new List<string>(); for (int i = 0; i < md5buffer.Length;i++ ) { md5Str += md5buffer[i].ToString("x2"); } return md5Str; } }}

四.吧啦吧啦

WebApplication2 的項目與 WindowsFormsApplication1 可以分開寫于不同的解決方案里。勿噴!抄寫.學習于 [此處](http://www.tuicool.com/articles/Z3mAvyF)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美自拍| 欧美高清第一页 | 成年人黄色免费电影 | 狠狠干天天 | 成人福利软件 | av在线免费看网址 | 黄色一级片在线观看 | 空姐一级毛片 | 国产精品久久久久久久久粉嫩 | 国产精品视频2021 | 免费一区二区三区 | 91女上位 在线播放 性欧美日本 | 一级免费黄色免费片 | 涩涩激情网 | 91av在线影院 | 毛片毛片免费看 | 成人三级黄色片 | 日韩精品久久久久久久九岛 | 久久精品视频在线免费观看 | 国产女厕一区二区三区在线视 | 国产女同玩人妖 | 免费观看一区二区三区 | 日本欧美一区二区三区在线观看 | 久久日本 | 最新久久免费视频 | 黄 色 免费网 站 成 人 | 中文字幕在线观看1 | 亚洲视频在线观看免费 | 激情网站视频 | 国产成人高清成人av片在线看 | 久久精品中文字幕一区二区 | 99激情 | 久章草影院 | 中午日产幕无线码1区 | 成人免费在线视频播放 | 久草在线高清视频 | 久久国产精品无码网站 | 一区二区三级视频 | 日韩黄色av网站 | 欧美精品一区二区视频 | asian gaysex|