C#实现HTTP上传文件的方法
作者:shichen2014 时间:2023-04-28 04:05:30
本文实例讲述了C#实现HTTP上传文件的方法。分享给大家供大家参考。具体实现方法如下:
发送文件代码如下:
/// <summary>
/// Http上传文件
/// </summary>
public static string HttpUploadFile(string url, string path)
{
// 设置参数
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
int pos = path.LastIndexOf("\\");
string fileName = path.Substring(pos + 1);
//请求头部信息
StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] bArr = new byte[fs.Length];
fs.Read(bArr, 0, bArr.Length);
fs.Close();
Stream postStream = request.GetRequestStream();
postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
postStream.Write(bArr, 0, bArr.Length);
postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
postStream.Close();
//发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream instream = response.GetResponseStream();
StreamReader sr = new StreamReader(instream, Encoding.UTF8);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
return content;
}
接收文件的代码如下:
using System;
using System.Web;
namespace SWX
{
public partial class test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpPostedFile file = Request.Files[0];
file.SaveAs(MapPath("\\UploadFile\\" + file.FileName));
Response.Write("Success\r\n");
}
}
}
希望本文所述对大家的C#程序设计有所帮助。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SSM框架实现分页和搜索分页的示例代码
c# RSA非对称加解密及XML&PEM格式互换方案
C#实现Zip压缩目录中所有文件的方法
Android如何从实现到封装一个MVP详解
![](https://img.aspxhome.com/file/2023/5/138775_0s.png)
Java代码注释规范(动力节点整理)
TOMCAT内存溢出及大小调整的实现方法
基于C语言string函数的详解
JAVA中实现原生的 socket 通信机制原理
![](https://img.aspxhome.com/file/2023/4/60484_0s.png)
详解处理Java中的大对象的方法
![](https://img.aspxhome.com/file/2023/1/81911_0s.jpg)
Java多线程实现四种方式原理详解
Android开发中解析xml文件XmlUtils工具类与用法示例
Java动态线程池插件dynamic-tp集成zookeeper
Android实现TCP断点上传 后台C#服务接收
SpringMVC中的Model对象用法说明
并行Stream与Spring事务相遇会发生什么?
![](https://img.aspxhome.com/file/2023/4/85944_0s.webp)
Java 动态模拟操作系统进程调度算法
![](https://img.aspxhome.com/file/2023/9/125669_0s.png)
Spring Security 中如何让上级拥有下级的所有权限(案例分析)
![](https://img.aspxhome.com/file/2023/9/68019_0s.jpg)
Java 数组高频考点分析讲解
![](https://img.aspxhome.com/file/2023/2/61152_0s.png)
Java多线程:生产者与消费者案例
![](https://img.aspxhome.com/file/2023/0/89940_0s.png)