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 ...
随机推荐
- jQuery动态移除和绑定事件
function bindEvent() { //移除绑定事件 $('.btnsp').unbind('click'); //绑定事件 $('.btnsp').bind('click', functi ...
- Hadoop架构模型
1.hadoop 1.x架构模型:分布式文件存储系统:HDFSNameNode(主节点:管理元数据) secondaryNameNode(作用是合并元数据信息,辅助NameNode管理元数据信息)Da ...
- Android图像处理之BitMap(2)
Bitmap 相关 1. Bitmap比较特别 因为其不可创建 而只能借助于BitmapFactory 而根据图像来源又可分以下几种情况: * png图片 如:R.drawable.tianjin J ...
- 将java project打包成jar包,web project 打包成war包的几种演示 此博文包含图片
转: http://blog.csdn.net/christine_ruan/article/details/7491559 http://developer.51cto.com/art/200907 ...
- 导出网页表格数据为Excel文件的前端解决方案
在工作中,我们有时会遇到这样的需求,比如:要把页面的表格数据导出为Excel文件.在此记录下自己用的解决方法.代码如下: function tableToExcel(data){ //要导出的数据,t ...
- P1759 通天之潜水(不详细,勿看)(动态规划递推,组合背包,洛谷)
题目链接:点击进入 题目分析: 简单的组合背包模板题,但是递推的同时要刷新这种情况使用了哪些物品 ac代码: #include<bits/stdc++.h> using namespace ...
- DataBase安装及简单配置
下载以及安装 打开mysql官网下载页面:http://dev.mysql.com/downloads/mysql/ 用管理员身份打开cmd命令行工具,cd到解压文件的bin目录 输入mysqld i ...
- Leetcode 220.存在重复元素III
存在重复元素III 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ. ...
- ZOJ1004 && HDU1515 dfs回溯
题目大意: 就是通过一个栈进行字母入栈出栈得到想要的字符,把所有可能的方式全部输出 自己写的方法一开始一直不能过,后来参考了别人的方法,写出来的比较简单的代码 这段代码更有回溯的感觉,自己后来又把自己 ...
- SQL SERVER代理作业删除失败问题
在SQL Server 2005上遇到了先删除已运行维护计划后,再删除代理中由其产生的作业时,提示删除失败. DELETE 语句与 REFERENCE 约束"FK_subplan_job ...