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 ...
随机推荐
- count() 方法
count() :方法用于统计字符串里某个字符出现的次数.可选参数为在字符串搜索的开始与结束位置. num1,num2 = input('请输入字符串:'),input('请输入要查询的子串:') p ...
- 执行jar包报错:udfFull.jar中没有主清单属性
在windows系统的cmd命令行窗口中执行: java -jar udfFull.jar {"movie":"1287","rate":& ...
- 获取本地验证码cookie
window.document.onkeydown = function (evt) { evt = (evt) ? evt : window.event; if (evt.keyCode) { if ...
- Vue-cli 3.0自定义脚手架
一.进入项目地址 https://github.com/vuejs/vue-cli ,选择 docs目录查看具体安装流程. 中文文档:https://cli.vuejs.org/zh 可以看到我电脑上 ...
- Windows——bat中的路径和工具栏运行bat的坑
工具栏添加的批处理环境 编写一个简单的批处理文件 set testEnv = %cd% pause 这里第一句:设置当前文件夹路径为环境变量testEnv的值 这里第二句:暂停命令窗口 第一次我们直接 ...
- PHP解惑(一)
PHP给人的印象是入门简单的语言.当你的技术能力达到一定阶段时,会发现情况并非如此. PHP采用"极简主义",就是以入门容易为准则设计的,在十几年的持续发展历程中,它早已成为一个开 ...
- 每日命令:(12)sar
sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告, 包括:文件的读写情况.系统调用的使用 ...
- Yum:更换aliyun的yum源
备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup centos7 wget -O /et ...
- ehcache、memcache、redis三大缓存比较
最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今天特意归纳下各个缓存的优缺点,仅供参考! Ehcache 在Java项目广泛的使用.它是一个开源的.设计于提高在数据从RDBMS ...
- Linux下汇编语言学习笔记23 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...