客户端(Winform窗体)上传文件到服务器(web窗体)简单例子
客户端:先创建一个winform窗体的应用程序项目
项目结构
winform窗体布局分配好,需要一个文本框,两个按钮,一个进度条,几个label标签
具体如下:
客户端代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UpFileClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string filePath = "";
private void btnSelect_Click(object sender, EventArgs e)
{
//创建文件弹出选择窗口(包括文件名)对象
OpenFileDialog ofd = new OpenFileDialog();
//判断选择的路径
if (ofd.ShowDialog() == DialogResult.OK)
{
this.txtSoundPath.Text = ofd.FileName.ToString();
}
filePath = this.txtSoundPath.Text;
}
private void btnUpSound_Click(object sender, EventArgs e)
{
try {
//上传服务器的地址(web服务)
string address = "http://localhost:28207/WebService/SaveFileWebForm.aspx";
//上传后文件保存的名称
string saveName = DateTime.Now.ToString("yyyyMMddHHmmss");
int count = UpSound_Request(address, filePath, saveName, this.progressBar1);
if (count > 0)
{
MessageBox.Show("上传文件成功!");
}
else
{
MessageBox.Show("上传文件失败!");
}
}catch(Exception ex){
MessageBox.Show(""+ex.GetBaseException());
}
}
/// <summary>
/// 上传录音文件
/// </summary>
/// <param name="address">文件上传到服务器的路径</param>
/// <param name="fileNamePath">要上传的本地路径(全路径)</param>
/// <param name="saveName">文件上传后的名称</param>
/// <returns>成功返回1,失败返回2</returns>
public int UpSound_Request(string address, string fileNamePath, string saveName, ProgressBar progressBar)
{
int returnValue = 0;
//要上传的文件
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
//二进制对象
BinaryReader r = new BinaryReader(fs);
//时间戳
string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n");
//请求的头部信息
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(strBoundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append("file");
sb.Append("\"; filename=\"");
sb.Append(saveName);
sb.Append("\";");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append("application/octet-stream");
sb.Append("\r\n");
sb.Append("\r\n");
string strPostHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
// 根据uri创建HttpWebRequest对象
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(address));
httpReq.Method = "POST";
//对发送的数据不使用缓存
httpReq.AllowWriteStreamBuffering = false;
//设置获得响应的超时时间(300秒)
httpReq.Timeout = 300000;
httpReq.ContentType = "multipart/form-data; boundary=" + strBoundary;
long length = fs.Length + postHeaderBytes.Length + boundaryBytes.Length;
long fileLength = fs.Length;
httpReq.ContentLength = length;
try
{
progressBar.Maximum = int.MaxValue;
progressBar.Minimum = 0;
progressBar.Value = 0;
//每次上传4k
int bufferLength = 4096;
byte[] buffer = new byte[bufferLength]; //已上传的字节数
long offset = 0; //开始上传时间
DateTime startTime = DateTime.Now;
int size = r.Read(buffer, 0, bufferLength);
Stream postStream = httpReq.GetRequestStream(); //发送请求头部消息
postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
while (size > 0)
{
postStream.Write(buffer, 0, size);
offset += size;
progressBar.Value = (int)(offset * (int.MaxValue / length));
TimeSpan span = DateTime.Now - startTime;
double second = span.TotalSeconds;
labTime.Text = "已用时:" + second.ToString("F2") + "秒";
if (second > 0.001)
{
labSpeed.Text = "平均速度:" + (offset / 1024 / second).ToString("0.00") + "KB/秒";
}
else
{
labSpeed.Text = " 正在连接…";
}
labState.Text = "已上传:" + (offset * 100.0 / length).ToString("F2") + "%";
labSize.Text = (offset / 1048576.0).ToString("F2") + "M/" + (fileLength / 1048576.0).ToString("F2") + "M";
Application.DoEvents();
size = r.Read(buffer, 0, bufferLength);
}
//添加尾部的时间戳
postStream.Write(boundaryBytes, 0, boundaryBytes.Length);
postStream.Close();
//获取服务器端的响应
WebResponse webRespon = httpReq.GetResponse();
Stream s = webRespon.GetResponseStream();
//读取服务器端返回的消息
StreamReader sr = new StreamReader(s);
String sReturnString = sr.ReadLine();
s.Close();
sr.Close();
if (sReturnString == "Success")
{
returnValue = 1;
}
else if (sReturnString == "Error")
{
returnValue = 0;
}
}
catch
{
returnValue = 0;
}
finally
{
fs.Close();
r.Close();
}
return returnValue;
}
}
}
客户端上传文件的效果图:
服务器端:
创建一个web mvc4项目,在创建一个webservice文件夹,在文件夹下创建一个SaveFileWebForm.axpx接口,
设置这个页面为项目起始页,打开这个页面,右键查看代码,编写服务器端代码,运行项目
项目结构:
客户端上传文件时,服务器端(SaveFileWebForm.axpx)需要访问的到,(注:先运行服务器程序项目,在运行客户端程序项目)
服务器端源码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SaveFileWebService.WebService
{
public partial class SaveFileWebForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Files.Count > 0)
{
try
{
//得到客户端上传的文件
HttpPostedFile file = Request.Files[0];
//服务器端要保存的路径
string filePath = "D:\\Test\\" + file.FileName + ".mp3";
file.SaveAs(filePath);
//返回结果
Response.Write("Success");
}
catch
{
Response.Write("Error");
}
}
else
{
Response.Write("Error1");
}
}
}
}
服务器接收后文件如下图:
PS: 客户端上传文件到服务器端的demo下载地址:http://pan.baidu.com/s/1nuSP3c9