当前位置: 首页 > news >正文

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#HTTP文件上传和参数_c# http上传文件-CSDN博客

http://www.lryc.cn/news/335275.html

相关文章:

  • pdffactory pro 8注册码序列号下载 附教程
  • 软件供应链安全:寻找最薄弱的环节
  • Training - Kubeflow 的 PyTorchJob 配置 DDP 分布式训练 (ncclInternalError)
  • java Web在线考试管理系统用eclipse定制开发mysql数据库BS模式java编程jdbc
  • 爬虫 新闻网站 以湖南法治报为例(含详细注释) V4.0 升级 自定义可任意个关键词查询、时间段、粗略判断新闻是否和优化营商环境相关,避免自己再一个个判断
  • 科技云报道:从“奇点”到“大爆炸”,生成式AI开启“十年周期”
  • 【用户案例】太美医疗基于Apache DolphinScheduler的应用实践
  • 权限管理系统【BUG】
  • 【CPA考试】2024注册会计师报名照片尺寸要求解读及手机拍照方法
  • 高并发环境下的实现与优化策略
  • 华为海思校园招聘-芯片-数字 IC 方向 题目分享——第二套
  • UML2.0在系统设计中的实际使用情况
  • django celery 异步任务 异步存储
  • apex0.1版本安装踩坑指南
  • HTML — 弹性布局(2)
  • MYSQL 8.0版本修改用户密码(知道登录密码)和Sqlyog错误码2058一案
  • Linux中磁盘管理
  • tailwindcss在manoca在线编辑智能感知
  • 通过本机调试远端路由器非直连路由
  • React路由快速入门:Class组件和函数式组件的使用
  • Pytorch数据结构:GPU加速
  • OpenHarmony开发-连接开发板调试应用
  • RabbitMQ如何保证消息的幂等性???
  • 【QT】Qt Charts的实际使用中的一些小细节完善如:resetZoom、fitInView
  • 上位机图像处理和嵌入式模块部署(qmacvisual缺失的颜色检测)
  • macOS制作C/C++ app
  • 学习笔记——C语言基本概念链表——(12)
  • 开发环境解决跨域问题
  • 物理服务器与云服务器的租用对比
  • Ubuntu Desktop Server - Resource temporarily unavailable