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, , postHeaderBytes.Length); // Stream the file contents in small pieces (4096 bytes, max).
byte[] buffer = new Byte[checked((uint)Math.Min(, (int)oFileStream.Length))];
int bytesRead = ;
while ((bytesRead = oFileStream.Read(buffer, , buffer.Length)) != )
oRequestStream.Write(buffer, , bytesRead);
oFileStream.Close(); // Add the trailing boundary
oRequestStream.Write(boundaryBytes, , 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");
}
}
}
}

  亲测可用

c# Winform上传文件的更多相关文章

  1. winform 上传文件

    using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...

  2. winform上传文件

    //上传图片 文件 public int addUpPic( String strProCode,String strFileName,String strUpType) { //strFileNam ...

  3. WinForm上传文件,下载文件

    上传文件: 使用OpenFileDialog控件选择文件, 具体代码示例: private void btnUpLoadPic_Click(object sender, EventArgs e) { ...

  4. winform上传文件,利用http,form-data格式上传

    /// <summary> /// 上传文件 /// </summary> /// <param name="url">服务地址</par ...

  5. C# winform 上传文件到服务器

    1.首先要在服务器端新建一个网站axpx页 然后再网站的后台写代码获取winform传过来的文件名. 声明:这个方法虽然最简单最省事,但是上传大文件可能会报错,我的机器是10M, 超过10M就会提示报 ...

  6. winform上传文件到服务器——资料整理

    标题:使用简单的wcf文件实现上传,下载文件到服务器 地址:https://blog.csdn.net/duanzi_peng/article/details/19037777

  7. 客户端(Winform窗体)上传文件到服务器(web窗体)简单例子

    客户端:先创建一个winform窗体的应用程序项目 项目结构

  8. Winform上传下载文件代码

    using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...

  9. C#在WinForm下使用HttpWebRequest上传文件

    转自:http://blog.csdn.net/shihuan10430049/article/details/3734398 这段时间因项目需要,要实现WinForm下的文件上传,个人觉得采用FTP ...

随机推荐

  1. springboot Tomcat connector configured to listen on port 8081 failed to start.

    启动报 Tomcat connector configured to listen on port 8081 failed to start.   The port may already be in ...

  2. 第2节 mapreduce深入学习:6、MapReduce当中的计数器

    第2节 mapreduce深入学习:6. MapReduce当中的计数器 计数器是收集作业统计信息的有效手段之一,用于质量控制或应用级统计.计数器还可辅助诊断系统故障.如果需要将日志信息传输到map ...

  3. Java中9大内置基本数据类型Class实例和数组的Class实例(转载)

    https://www.jianshu.com/p/58976c8bf1e1

  4. PHP明细之间的关联和having进行分组,不推荐这样做,只是做为偷懒的办法

    -- 只求和wrt的数据,其它数据保持不变!SELECT A.return_id,A.relevant_id,A.order_id,A.deliver_order_id,A.product_id,A. ...

  5. 配置jdk环境变量和配置的作用

    对于JDK要配置三个环境变量,分别是JAVA_HOME.path.classpath 对于我本人电脑来说,配置如下: JAVA_HOME:C:\Program Files\Java\jdk1.8.0_ ...

  6. 树莓派 - 通过sysfs操控GPIO

    点亮或熄灭LED 硬件上,一个LED灯接在pi的Pin-25.  该引脚为BCM的GPIO26 $ gpio readall +-----+-----+---------+------+---+--- ...

  7. php-fpm进程数管理

    PHP-FPM 先来了解一些名词概念: CGI是Common Gateway Interface(通用网管协议),用于让交互程序和Web服务器通信的协议.它负责处理URL的请求,启动一个进程,将客户端 ...

  8. Python xml文件处理

    什么是XML文件? xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 从结构上,很像HTML超文本标记语言.但他们被设计的目的是不同的,具体如 ...

  9. Selenium IDE-自动化实战

    1.输入测试 Base URL 2.打开录制按钮(默认是录制状态,点击一下就是停止,再次点击,又继续录制) 3.在浏览器界面进行相关操作(比如输入selenium,点击搜索按钮,查看搜索结果),之后点 ...

  10. Codeforces870F. Paths

    n<=10000000的图,满足:如果(i,j)>1就连一条边权1的无相变,问所有d(u,v) (u<=v)--u到v的最短路之和. 首先1和>n/2的质数都是孤立的点.然后两 ...