首页 > 设计 > WEB开发 > 正文

C# __ WebService上传文件

2019-11-02 18:26:25
字体:
来源:转载
供稿:网友

一. 项目截图

这里写图片描述

二. 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("//"); 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)
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表