我是靠谱客的博主 炙热香氛,最近开发中收集的这篇文章主要介绍c# 使用WebRequest实现多文件上传,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

c#中通常使用HttpWebRequest进行HTTP网络请求,HttpWebRequest只对Http请求进行了最简单的封装。如果要利用Http协议实现多文件上传,则必须使用POST方法multipart/form-data格式。为了重复使用,我封装了几个方法,实现了多参数文件上传。

添加引用

使用WebRequest需要添加引用System.Web,否则引入出错。

参数封装

方便起见,我把请求参数进行了封装,代码如下:

namespace EasyHttp.Net.Core
{
  public class KeyValue
  {
    public string Key;
    public string Value;
    public string FilePath;
    public string ContentType="*/*";

    public KeyValue(string key, string value, string filePath, string contentType)
    {
      Key = key;
      Value = value;
      FilePath = filePath;
      ContentType = contentType;
    }

    public KeyValue() { }

    public KeyValue(string key, string value, string filePath)
    {
      Key = key;
      Value = value;
      FilePath = filePath;
    }

    public KeyValue(string key, string value)
    {
      Key = key;
      Value = value;
    }
  }
}

KeyValue代表了广义的参数,可以是普通的键值对,也可以是文件参数。

多文件上传封装

public static void ExecuteMultipartRequest(HttpWebRequest request, List<KeyValue> nvc)
{
  Console.WriteLine(request.Headers);
  //  log.Debug(string.Format("Uploading {0} to {1}", file, url));
  string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
  byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("rn--" + boundary + "rn");

  HttpWebRequest wr = request;
  wr.ContentType = "multipart/form-data; boundary=" + boundary;
  wr.Method = "POST";
  wr.KeepAlive = true;
  wr.Credentials = System.Net.CredentialCache.DefaultCredentials;



  using (var rs = wr.GetRequestStream())
  {
    // 普通参数模板
    string formdataTemplate = "Content-Disposition: form-data; name="{0}"rnrn{1}";
    //带文件的参数模板
    string headerTemplate = "Content-Disposition: form-data; name="{0}"; filename="{1}"rnContent-Type: {2}rnrn";
    foreach (KeyValue keyValue in nvc)
    {
      //如果是普通参数
      if (keyValue.FilePath == null)
      {
        rs.Write(boundarybytes, 0, boundarybytes.Length);
        string formitem = string.Format(formdataTemplate, keyValue.Key, keyValue.Value);
        byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
        rs.Write(formitembytes, 0, formitembytes.Length);
      }
      //如果是文件参数,则上传文件
      else
      {
        rs.Write(boundarybytes, 0, boundarybytes.Length);

        string header = string.Format(headerTemplate, keyValue.Key, keyValue.FilePath, keyValue.ContentType);
        byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
        rs.Write(headerbytes, 0, headerbytes.Length);

        using (var fileStream = new FileStream(keyValue.FilePath, FileMode.Open, FileAccess.Read))
        {
          byte[] buffer = new byte[4096];
          int bytesRead = 0;
          long total = 0;
          while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
          {

            rs.Write(buffer, 0, bytesRead);
            total += bytesRead;
          }
        }
      }
      
    }

    byte[] trailer = System.Text.Encoding.ASCII.GetBytes("rn--" + boundary + "--rn");
    rs.Write(trailer, 0, trailer.Length);

  }

}

使用

static void Main(string[] args)
    {
	var request = WebRequest.Create("http://localhost:8080/test/upload") as HttpWebRequest;
      List<KeyValue> keyValues = new List<KeyValue>();
      keyValues.Add(new KeyValue("key1","param1"));
      keyValues.Add(new KeyValue("key2", "param2"));
      keyValues.Add(new KeyValue("file","test1.png","image/png"));
      keyValues.Add(new KeyValue("file", "test2.png", "image/png"));

      EasyHttp.ExecuteMultipartRequest(request,keyValues);
    }

以上就是c# 使用WebRequest实现多文件上传的详细内容,更多关于c# 多文件上传的资料请关注靠谱客其它相关文章!

最后

以上就是炙热香氛为你收集整理的c# 使用WebRequest实现多文件上传的全部内容,希望文章能够帮你解决c# 使用WebRequest实现多文件上传所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(157)

评论列表共有 0 条评论

立即
投稿
返回
顶部