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");
}
}
}
}

  亲测可用

最新文章

  1. MySQL行锁深入研究
  2. Caffe源码解析3:Layer
  3. 用avalon实现一个完整的todomvc(带router)
  4. ftl文件格式化jsp形式显示
  5. Insert BLOB && CLOB from PL/SQL and JDBC
  6. wordpress文章ID不连续显示问题的完美解决
  7. python3-day4(装饰器)
  8. jeecg 3.5.2 新版本号4种首页风格 【经典风格,shortcut风格,ACE bootstrap风格,云桌面风格】
  9. 使用IntelliJ IDEA开发SpringMVC网站(一)开发环境
  10. document.querySelectorAll() 与document.getElementTagName() 的区别
  11. EasyUi+Spring Data 实现按条件分页查询
  12. knockoutjs模板实现树形结构列表
  13. .Net 关于 InfoPath 的基本使用
  14. shell编辑crontab任务
  15. java.lang.NoClassDefFoundError:org/hamcrest/SelfDescribing
  16. Java中 try--catch-- finally、throw、throws 的用法
  17. Confluence 6 管理协同编辑 - 代理和 SSL 的考虑
  18. nacos-server集群 安装、运行(ubuntu)
  19. 20165336 2017-2018-2《Java程序设计》课程总结
  20. python pandas简单使用处理csv文件

热门文章

  1. qrcode.js扫码邀请
  2. vue 组件用法
  3. Bootstrap 12 栅格系统
  4. solr-5.3.1配置(win7 x64)
  5. 09-看图理解数据结构与算法系列(B树)
  6. Session与Token的区别
  7. 慕课笔记利用css进行布局【一列布局】
  8. 爬虫(1):requests模块
  9. centos6.4下安装mysql
  10. Frequent values(poj 3368)