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

c# Winform上传文件

http://blog.csdn.net/shihuan10430049/article/details/3734398这个代码有点问题

 

http://blogs.msdn.com/b/johan/archive/2006/11/15/are-you-getting-outofmemoryexceptions-when-uploading-large-files.aspx

 

http://blog.csdn.net/five3/article/details/7181521

Winform代码:

public static void UploadFile(string strFilePath, string strSavePath, string strURL)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);

            string strBoundry = "------------" + System.DateTime.Now.Ticks.ToString("x");
            // The trailing boundary string
            byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundry + "\r\n");
            StringBuilder sb = new StringBuilder();
            sb.Append("--");
            sb.Append(strBoundry);
            sb.Append("\r\n");
            sb.Append("Content-Disposition: form-data; name=\"");
            sb.Append("A.txt");
            sb.Append("\"; filename=\"");
            sb.Append(Path.GetFileName(strFilePath));
            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);

            // The WebRequest
            HttpWebRequest oWebrequest = (HttpWebRequest)WebRequest.Create(strURL);
            oWebrequest.ContentType = "multipart/form-data; boundary=" + strBoundry;
            oWebrequest.Method = "POST";

            // This is important, otherwise the whole file will be read to memory anyway...
            oWebrequest.AllowWriteStreamBuffering = false;

            // Get a FileStream and set the final properties of the WebRequest
            FileStream oFileStream = new FileStream(strFilePath, FileMode.Open, FileAccess.Read);
            long length = postHeaderBytes.Length + oFileStream.Length + boundaryBytes.Length;
            oWebrequest.ContentLength = length;
            Stream oRequestStream = oWebrequest.GetRequestStream();

            // Write the post header
            oRequestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

            // Stream the file contents in small pieces (4096 bytes, max).
            byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)oFileStream.Length))];
            int bytesRead = 0;
            while ((bytesRead = oFileStream.Read(buffer, 0, buffer.Length)) != 0)
                oRequestStream.Write(buffer, 0, bytesRead);
            oFileStream.Close();

            // Add the trailing boundary
            oRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
            WebResponse oWResponse = oWebrequest.GetResponse();
            Stream s = oWResponse.GetResponseStream();
            StreamReader sr = new StreamReader(s);
            String sReturnString = sr.ReadToEnd();

            // Clean up
            oFileStream.Close();
            oRequestStream.Close();
            s.Close();
            sr.Close();

            //return sReturnString;
 
        }

aspx代码

public partial class Save : 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 = this.MapPath("UploadDocument") + "\\" + file.FileName;
                    if (!Directory.Exists(Path.GetDirectoryName(filePath)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                    }
                    file.SaveAs(filePath);
                    Response.Write("Success/r/n");
                }
                catch(Exception ex)
                {
                    Response.Write("Error/r/n");
                }
            }
        }
    }

  亲测可用

转载于:https://www.cnblogs.com/niuge/p/3941989.html

相关文章:

  • Codeforces Round #532 (Div. 2)
  • Ubuntu 图形处理软件
  • 【每日笔记】【Go学习笔记】2019-01-10 codis proxy处理流程
  • 创建Windows服务简单流程
  • Airflow成为Apache软件基金会的顶级项目
  • BZOJ 3039: 玉蟾宫
  • JSP学习笔记(一)
  • 《TCP IP 详解卷1:协议》阅读笔记 - 第六章
  • 继承中代码执行顺序
  • 遍历日志文件并打印
  • Spartan6系列之器件引脚功能详述
  • 菠萝大象--sping
  • 腾讯视频格式如何转换成mp4 将下载的qlv文件转换成mp4的方法
  • 深入jQuery Mobile
  • 【翻译】babel对TC39装饰器草案的实现
  • 【翻译】Mashape是如何管理15000个API和微服务的(三)
  • Android单元测试 - 几个重要问题
  • Apache的基本使用
  • Docker 1.12实践:Docker Service、Stack与分布式应用捆绑包
  • Java 最常见的 200+ 面试题:面试必备
  • java中具有继承关系的类及其对象初始化顺序
  • Nginx 通过 Lua + Redis 实现动态封禁 IP
  • NLPIR语义挖掘平台推动行业大数据应用服务
  • Python进阶细节
  • vue.js框架原理浅析
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 前端技术周刊 2019-02-11 Serverless
  • 前嗅ForeSpider中数据浏览界面介绍
  • 温故知新之javascript面向对象
  • # 数据结构
  • ## 临床数据 两两比较 加显著性boxplot加显著性
  • #android不同版本废弃api,新api。
  • (22)C#传智:复习,多态虚方法抽象类接口,静态类,String与StringBuilder,集合泛型List与Dictionary,文件类,结构与类的区别
  • (PWM呼吸灯)合泰开发板HT66F2390-----点灯大师
  • (zz)子曾经曰过:先有司,赦小过,举贤才
  • (十八)用JAVA编写MP3解码器——迷你播放器
  • (转)真正的中国天气api接口xml,json(求加精) ...
  • .Net core 6.0 升8.0
  • .NET 使用 ILMerge 合并多个程序集,避免引入额外的依赖
  • .Net开发笔记(二十)创建一个需要授权的第三方组件
  • .set 数据导入matlab,设置变量导入选项 - MATLAB setvaropts - MathWorks 中国
  • .vollhavhelp-V-XXXXXXXX勒索病毒的最新威胁:如何恢复您的数据?
  • .vue文件怎么使用_vue调试工具vue-devtools的安装
  • @Mapper作用
  • [ Linux 长征路第五篇 ] make/Makefile Linux项目自动化创建工具
  • [20171102]视图v$session中process字段含义
  • [2024] 十大免费电脑数据恢复软件——轻松恢复电脑上已删除文件
  • [23] 4K4D: Real-Time 4D View Synthesis at 4K Resolution
  • [Android]Android开发入门之HelloWorld
  • [c++] 单例模式 + cyberrt TimingWheel 单例分析
  • [codevs 2822] 爱在心中 【tarjan 算法】
  • [hdu1561] The more, The Better 【树形DP】
  • [iOS开发]iOS中TabBar中间按钮凸起的实现
  • [LeetCode] Longest Common Prefix 字符串公有前序