发送代码: var imgFile = Request.Files["imgFile"]; if (imgFile == null) return; var img = Image.FromStream(imgFile.InputStream); var ms = new MemoryStream(); img.Save(ms,ImageFormat.Jpeg); var data = new byte[ms.Length]; ms.Seek(0, SeekOrigin.Begin); ms.Read(data, 0, Convert.ToInt32(ms.Length)); ms.Dispose(); var fileName = imgFile.FileName; var fileType = imgFile.ContentType; var fileSize = data.Length.ToString(); var myRequest = WebRequest.Create(Common.ImgUrl + "/upload/UploadImg.aspx"); myRequest.Method = "POST"; myRequest.ContentType = fileType; myRequest.ContentLength = data.Length; myRequest.Headers.Add("FileType", Server.UrlEncode(fileType)); myRequest.Headers.Add("FileSize", fileSize); myRequest.Headers.Add("FileName", Server.UrlEncode(fileName)); myRequest.Headers.Add("dir", Server.UrlEncode(Request["dir"])); using (var newStream = myRequest.GetRequestStream()) { // Send the data. newStream.Write(data, 0, data.Length); newStream.Close(); } // Get response var myResponse = myRequest.GetResponse(); […]
View Details//利用InputStream 属性直接从HttpPostedFile对象读取文本内容 System.IO.Stream MyStream; int FileLen; FileLen = file.ContentLength; // 读取文件的 byte[] byte[] bytes = new byte[FileLen]; MyStream = file.InputStream; MyStream.Read(bytes, 0, FileLen); from:http://blog.csdn.net/yyixin/article/details/5336899
View Details
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
// 待请求的地址 string url = "http://www.cnblogs.com"; // 创建 WebRequest 对象,WebRequest 是抽象类,定义了请求的规定, // 可以用于各种请求,例如:Http, Ftp 等等。 // HttpWebRequest 是 WebRequest 的派生类,专门用于 Http System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create(url) as System.Net.HttpWebRequest; // 请求的方式通过 Method 属性设置 ,默认为 GET // 可以将 Method 属性设置为任何 HTTP 1.1 协议谓词:GET、HEAD、POST、PUT、DELETE、TRACE 或 OPTIONS。 request.Method = "POST"; // 还可以在请求中附带 Cookie // 但是,必须首先创建 Cookie 容器 request.CookieContainer = new System.Net.CookieContainer(); System.Net.Cookie requestCookie = new System.Net.Cookie("Request", "RequestValue","/", "localhost"); request.CookieContainer.Add(requestCookie); Console.WriteLine("请输入请求参数:"); // 输入 POST 的数据. string inputData = Console.ReadLine(); // 拼接成请求参数串,并进行编码,成为字节 string postData = "firstone=" + inputData; ASCIIEncoding encoding = new ASCIIEncoding(); byte[] byte1 = encoding.GetBytes(postData); // 设置请求的参数形式 request.ContentType = "application/x-www-form-urlencoded"; // 设置请求参数的长度. request.ContentLength = byte1.Length; // 取得发向服务器的流 System.IO.Stream newStream = request.GetRequestStream(); // 使用 POST 方法请求的时候,实际的参数通过请求的 Body 部分以流的形式传送 newStream.Write(byte1, 0, byte1.Length); // 完成后,关闭请求流. newStream.Close(); // GetResponse 方法才真的发送请求,等待服务器返回 System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse(); // 首先得到回应的头部,可以知道返回内容的长度或者类型 Console.WriteLine("Content length is {0}", response.ContentLength); Console.WriteLine("Content type is {0}", response.ContentType); // 回应的 Cookie 在 Cookie 容器中 foreach (System.Net.Cookie cookie in response.Cookies) { Console.WriteLine("Name: {0}, Value: {1}", cookie.Name, cookie.Value); } Console.WriteLine(); // 然后可以得到以流的形式表示的回应内容 System.IO.Stream receiveStream = response.GetResponseStream(); // 还可以将字节流包装为高级的字符流,以便于读取文本内容 // 需要注意编码 System.IO.StreamReader readStream = new System.IO.StreamReader(receiveStream, Encoding.UTF8); Console.WriteLine("Response stream received."); Console.WriteLine(readStream.ReadToEnd()); // 完成后要关闭字符流,字符流底层的字节流将会自动关闭 response.Close(); readStream.Close(); |
from:http://www.cnblogs.com/haogj/archive/2011/06/09/2076708.html
View DetailsAuthor:xuzhihong Create Date:2011-06-03 Descriptions: WinForm程序使用HttpWebRequest实现大文件上传 概述: 通常在WinForm程序中都是采用WebClient方式实现文件上传功能,本身这个方式没有问题,但是当需要上传大文件比如说(300+M)的时候,那么WebClient将会报内存不足异常(Out of Memory Exceptions),究其原因是因为WebClient方式是一次性将整个文件全部读取到本地内存中,然后再以数据流形式发送至服务器。本文将讲述如何采用HttpWebRequest方式每次读取固定大小数据片段(如4KB)发送至服务器,为大文件上传提供解决方案,本文还将详细讲述将如何将“文件上传”功能做为用户自定义控件,实现模块重用。 关键词:HttpWebRequest、WebClient、OutOfMemoryExceptions 解决方案: 开始我在WinForm项目中实现文件上传功能的时候,是采用WebClient(WebClient myWebClient = new WebClient();)方式,这大部分情况都是正确的,但有时候会出现内存不足的异常(Out of Memory Exceptions),经常测试,发现是由于上传大文件的时候才导致这问题。在网上查阅了一下其他网友的解决方案,最后找的发生异常的原因:“WebClient方式是一次性将整个文件全部读取到本地内存中,然后再以数据流形式发送至服务器”,详细请参考:http://blogs.msdn.com/b/johan/archive/2006/11/15/are-you-getting-outofmemoryexceptions-when-uploading-large-files.aspx 。按照这个解释,那么大文件上传出现内存不足的异常也就不足为奇了。下面我将讲述如何一步步使用HttpWebRequest方式来实现文件分块上传数据流至服务器。 按照惯例还是先预览一下文件上传最后的效果吧,如下图所示: 界面分为两部分,上面是文件基本信息,下面是文件上传自定义控件,我这里实现的是一个案件上传多个监控视频功能。以下是详细步骤: 第一步:创建用户自定义控件BigFileUpload.xaml 文件上传是一个非常常用的功能,为了所写的程序能非常方便地多次重复使用,我决定将其处理为一个用户自定义控件(UserControl)。 我们先在项目中创建一个FileUpload文件夹,在其目录下新建一个WPF自定义控件文件命名为BigFileUpload.xaml,这样就表示文件上传是一个独立的小模块使用。之所以用WPF自定义控件是因为WPF页面效果好看点,而且我想以后可能大部分C/S程序都会渐渐的由WinForm转向WPF吧,当然创建Window Forms用户控件也是没有问题的。然后我们需要做一个下图效果的页面布局: 前台设计代码如下: <UserControl x:Class="CHVM.FileUpload.BigFileUpload" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="160" Width="480"> <Grid Height="160" Width="480" Background="White"> <Label Height="28" HorizontalAlignment="Left" Margin="16,10,0,0" Name="label1" VerticalAlignment="Top" Width="53">文件</Label> <Label HorizontalAlignment="Left" Margin="15,52,0,80" Name="label2" Width="54">进度</Label> <ProgressBar Height="20" Margin="61,52,116,0" Name="progressBar1" VerticalAlignment="Top" /> <TextBox Height="23" Margin="61,12,116,0" Name="txtBoxFileName" VerticalAlignment="Top" /> <Button Height="23" HorizontalAlignment="Right" Margin="0,10,35,0" Name="BtnBrowse" VerticalAlignment="Top" Width="75" Click="BtnBrowse_Click">浏览…</Button> <Button Height="23" HorizontalAlignment="Right" Margin="0,52,35,0" Name="BtnUpload" VerticalAlignment="Top" Width="75" Click="BtnUpload_Click">上传</Button> <Label HorizontalAlignment="Left" Margin="16,0,0,44" Name="lblState" Width="183" Height="35" […]
View Details一个网站中需要上传一个文件到另一个网站,可以使用HttpWebRequest或者WebClient。 但是WebClient需要首先上传文件到服务器,才能执行发送,不太符合我的需求,这里不再介绍。 通过HttpWebRequest发送的原理: 构建一个HttpWebRequest,通过FileUpload获取要上传的文件,通过字节流发送这个文件,另一个网站接收字节流,保存到服务器。 发送程序: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 //获取要上传的文件信息 byte[]data=fileupload1.FileBytes; stringfileName=fileupload1.FileName; stringfileType=fileupload1.PostedFile.ContentType; stringfileSize=data.Length.ToString(); HttpWebRequest myRequest=(HttpWebRequest)WebRequest.Create("http://localhost:8102/Default.aspx"); myRequest.Method="POST"; myRequest.ContentType=fileType; myRequest.ContentLength=data.Length; myRequest.Headers.Add("FileType",Server.UrlEncode(fileType)); myRequest.Headers.Add("FileSize",fileSize); myRequest.Headers.Add("FileName",Server.UrlEncode(fileName)); using(Stream newStream=myRequest.GetRequestStream()) { // Send the data. newStream.Write(data,0,data.Length); newStream.Close(); } // Get response HttpWebResponse myResponse=(HttpWebResponse)myRequest.GetResponse(); StreamReader reader=newStreamReader(myResponse.GetResponseStream(),Encoding.UTF8); stringcontent=reader.ReadToEnd(); 接收程序: 0 1 2 3 4 5 6 7 8 9 10 stringfileName=Server.UrlDecode(Request.Headers["FileName"].ToString()); stringfileType=Server.UrlDecode(Request.Headers["FileType"].ToString()); intfileSize=int.Parse(Request.Headers["FileSize"].ToString()); byte[]bytes=Request.BinaryRead(fileSize); File.WriteAllBytes(Server.MapPath("~/uploadfiles/"+fileName),bytes); Response.HeaderEncoding=System.Text.Encoding.UTF8; Response.Charset="utf-8"; Response.Write("FileType:"+fileType+";FileName:"+fileName+";FileSize:"+fileSize); FROM:http://blog.bossma.cn/dotnet/asp-net-httpwebrequest-upload-send-file/
View Details