c# Winform上传文件
http://blog.csdn.net/shihuan10430049/article/details/3734398这个代码有点问题
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上传文件的更多相关文章
- winform 上传文件
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...
- winform上传文件
//上传图片 文件 public int addUpPic( String strProCode,String strFileName,String strUpType) { //strFileNam ...
- WinForm上传文件,下载文件
上传文件: 使用OpenFileDialog控件选择文件, 具体代码示例: private void btnUpLoadPic_Click(object sender, EventArgs e) { ...
- winform上传文件,利用http,form-data格式上传
/// <summary> /// 上传文件 /// </summary> /// <param name="url">服务地址</par ...
- C# winform 上传文件到服务器
1.首先要在服务器端新建一个网站axpx页 然后再网站的后台写代码获取winform传过来的文件名. 声明:这个方法虽然最简单最省事,但是上传大文件可能会报错,我的机器是10M, 超过10M就会提示报 ...
- winform上传文件到服务器——资料整理
标题:使用简单的wcf文件实现上传,下载文件到服务器 地址:https://blog.csdn.net/duanzi_peng/article/details/19037777
- 客户端(Winform窗体)上传文件到服务器(web窗体)简单例子
客户端:先创建一个winform窗体的应用程序项目 项目结构
- Winform上传下载文件代码
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...
- C#在WinForm下使用HttpWebRequest上传文件
转自:http://blog.csdn.net/shihuan10430049/article/details/3734398 这段时间因项目需要,要实现WinForm下的文件上传,个人觉得采用FTP ...
随机推荐
- JavaSE-09 继承
学习要点 继承的优点和实现 子类重写父类方法 继承下构造方法的执行过程 抽象类和抽象方法的使用 final修饰属性.方法和类 继承的优点和实现 宠物管理系统优化设计 宠物管理系统中的类有什么问题? 使 ...
- hdfs的特性、命令、安全模式、基准测试
1.第一点:如何理解hdfs分布式文件系统,每台机器出一块磁盘,凑成一个大的硬盘,大的硬盘的容量来自各个服务器的硬盘容量之和. 你出5毛,我出5毛,大家凑成1块. 2. HDFS 是 Hadoop D ...
- 离线缓存 application cache
1. 什么是离线缓存: 离线缓存可以将站点的一些文件缓存到本地,它是浏览器自己的一种机制,将需要的文件缓存下来,以便后期即使没有连接网络,被缓存的页面也可以展示. 例子:比如我们在手机或电脑上访问一个 ...
- 记一次被面试的final问题
---- 前言 今天面试被问到了,我们都知道final修饰的东西是不可变的,那么是值不可变还是其地址不可变?一脸懵逼,回来查阅一番,总结一下 --- final与数据 在日常行为下,一般数据指的都是基 ...
- LNMP中PHP服务的配置
PHP(Hypertxt Preprocessor,超文本预处理器)是一种通用的开源脚本语言,发明于1995年,它吸取了C语言.Java语言及Perl语言的很多优点,具有开源.免费.快捷.跨平台性强. ...
- Wireshark does not show SSL/TLS
why it doesn't show as "TLS/SSL"? Because it's not on the standard port for SSL/TLS. You c ...
- PHP:POST OR GET 请求
文章来源:http://www.cnblogs.com/hello-tl/p/7685216.html /** * 模拟提交参数,支持https提交 可用于各类api请求 * @param strin ...
- UVA 1596 Bug Hunt (大模拟 栈)
题意: 输入并模拟执行一段程序,输出第一个bug所在的行. 每行程序有两种可能: 数组定义: 格式为arr[size]. 例如a[10]或者b[5],可用下标分别是0-9和0-4.定义之后所有元素均为 ...
- LeetCode 123. Best Time to Buy and Sell Stock III (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Webbrowser 在web项目中的使用
string htmlstr = string.Empty; [STAThread] public string GetHtmlByWeb(string url) { try { RunWithSin ...