如何在Asp.Net中利用表单实现一个文件上传功能

  介绍

今天就跟大家聊聊有关如何在Asp。净中利用表单实现一个文件上传功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

<强> WebClient上传只包含键值对的文本信息示例代码:

代码如下:字符串uriString=癶ttp://localhost/login.aspx";
//创建一个新的WebClient实例。
WebClient myWebClient=new WebClient ();
字符串postData=https://www.yisu.com/zixun/坝没?管理密码=admin”;
//注意这种拼字符串的ContentType
myWebClient.Headers.Add (“Content-Type",“应用程序/x-www-form-urlencoded");
//转化成二进制数组
字节中byteArray=[] Encoding.ASCII.GetBytes (postData);
//上传数据,并获取返回的二进制数据。
byte [] responseArray=myWebClient.UploadData (uriString,“POST"中byteArray);

<强> WebClient上传只包含文件的示例代码:

,代码如下:字符串uriString=癶ttp://localhost/uploadFile.aspx";
//创建一个新的WebClient实例。
WebClient myWebClient=new WebClient ();
字符串文件名=@" C:/upload.txt";
//直接上传,并获取返回的二进制数据。
byte [] responseArray=myWebClient.UploadFile (uriString,“POST"文件名),

对于既包含文件又包含文本键值对信息的示例代码,需要构造表单提交的内容,对于学asp的同学来说,下面的表单提交内容一定不会陌生

代码如下:- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 7 d429871607fe
附加项:格式;name=癴ile1";文件名=癎:/homepage.txt"
内容类型:文本/纯
: https://www.jb51.net
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 7 d429871607fe
附加项:格式;name=癴ilename"
默认文件名
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 7 d429871607fe——

所以只要拼一个这样的byte[]数据数据帖子过去,就可以达到同样的效果了。但是一定要注意,对于这种带有文件上传的,其ContentType是不一样的,例如上面的这种,其ContentType为“多部分/格式;边界=- - - - - - - - - - - - - - - - - - - - - - - - - - - - 7 d429871607fe"。有了ContentType,我们就可以知道边界(就是上面的“- - - - - - - - - - - - - - - - - - - - - - - - - - - - 7 d429871607fe"),知道边界了我们就可以构造出我们所需要的byte[]数据了,最后,不要忘记,把我们构造的ContentType传到WebClient中(例如:webClient.Headers.Add (“Content-Type" ContentType);)这样,就可以通过WebClient。UploadData方法上载文件数据了。

使用系统;
  使用包含;
  使用先;
  使用System.Net;
  使用text;
  使用System.Collections;
  名称空间UploadData.Common
  {
  公开课CreateBytes
  {
  编码编码=Encoding.UTF8;
  公共byte [] JoinBytes (ArrayList中bytearray)
  {
  int长度=0;
  int readLength=0;//加上结束边界
  字符串endBoundary=边界+“——“;
  byte [] endBoundaryBytes=encoding.GetBytes (endBoundary);
  byteArrays.Add (endBoundaryBytes);
  foreach (byte[]中bytearray b)
  {
  +=b.Length长度;
  }
  byte[]字节=新字节(长度);//遍历复制
  foreach (byte[]中bytearray b)
  {
  b。CopyTo(字节,readLength);
  readLength +=b.Length;
  }
  返回字节;
  }
  uploadUrl公共bool UploadData(字符串,byte[]字节,字节[]responseBytes)
  {
  WebClient WebClient=new WebClient ();
  webClient.Headers.Add (“Content-Type" ContentType);
  试一试
  {
  responseBytes=webClient。UploadData (uploadUrl字节);
  返回true;
  }
  抓住(WebException练习)
  {
  流resp=ex.Response.GetResponseStream ();
  responseBytes=新字节(ex.Response.ContentLength);
  分别地。读(responseBytes 0 responseBytes.Length);
  }
  返回错误;
  }///获取普通表单区域二进制数组
  公共byte [] CreateFieldData(字段名的字符串,字符串fieldValue)
  {
  字符串textTemplate=边界+“;附加:格式;name=皗0}”;{1}“;=字符串文本字符串。字段名,格式(textTemplate fieldValue);
  byte[]字节=encoding.GetBytes(文本);
  返回字节;
  }
  公共byte [] CreateFieldData(文件名字符串字段名,字符串,字符串contentType, byte [] fileBytes)
  {
  字符串结束=?“;
  字符串textTemplate=边界+“;附加:格式;name=皗0}”;文件名=皗1}”;内容类型:{2}“;//头数据
  字符串数据=https://www.yisu.com/zixun/String.Format (textTemplate,字段名,文件名,contentType);
  byte[]字节=encoding.GetBytes(数据);//尾数据
  byte [] endBytes=encoding.GetBytes(结束);//合成后的数组
  byte [] fieldData=new字节(字节。+ fileBytes长度。长度+ endBytes.Length];
  字节。CopyTo (fieldData 0);//头数据
  fileBytes。CopyTo (fieldData bytes.Length);//文件的二进制数据
  endBytes。CopyTo (fieldData字节。长度+ fileBytes.Length);//返回fieldData;
  }
  公共字符串边界
  {
  得到
  {
  string [] bArray ctArray;
  字符串contentType=contentType;
  ctArray=contentType.Split (“;”);
  如果(ctArray [0] .Trim () .ToLower()==岸嗖糠?格式”)
  {
  bArray=ctArray [1] .Split ('=');
  返回”——“+ bArray [1];
  }
  返回null;
  }
  }
  公共字符串ContentType
  {
  得到
  {
  如果(HttpContext。当前==null)
  {
  返回“多部分/格式;边界=- - - - - - - - - - - - - - - - - - - - - - - - - - - - 7 d5b915500cee”;
  }
  返回HttpContext.Current.Request.ContentType;
  }
  }
  }
  }
  使用系统;
  使用System.Drawing;
  使用System.Collections;
  使用System.ComponentModel;
  使用System.Windows.Forms;
  使用System.Data;
  使用UploadData.Common;
  使用先;
  名称空间UploadDataWin
  {
  公开课frmUpload: System.Windows.Forms.Form
  {
  私人System.Windows.Forms。标签lblAmigoToken;
  私人System.Windows.Forms。文本框txtAmigoToken;
  私人System.Windows.Forms。标签lblFilename;
  私人System.Windows.Forms。文本框txtFilename;
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null

如何在Asp.Net中利用表单实现一个文件上传功能