国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > WEB開發(fā) > 正文

C# __ WebService上傳文件

2024-04-27 15:16:58
字體:
供稿:網(wǎng)友

一. 項目截圖

這里寫圖片描述

二. 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 從腳本中調(diào)用此 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 { //首先設置上傳服務器文件的路徑 然后發(fā)布web服務 發(fā)布的時候要自己建一個自己知道的文件夾 "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 = "地圖數(shù)據(jù)文件(*.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)
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 封丘县| 宣武区| 和林格尔县| 孙吴县| 清涧县| 霍林郭勒市| 旬邑县| 甘肃省| 辉南县| 红河县| 厦门市| 铜川市| 磐安县| 博兴县| 碌曲县| 巴马| 梧州市| 广汉市| 合水县| 潮州市| 鹰潭市| 原阳县| 石林| 三门峡市| 新巴尔虎左旗| 隆尧县| 江西省| 烟台市| 巴里| 德惠市| 永川市| 内丘县| 屏东市| 西青区| 定南县| 广德县| 广元市| 旬阳县| 抚宁县| 呼伦贝尔市| 南华县|