nopcommerce中文网 nopcommerce是国外asp.net领域一个高质量的b2c开源项目,基于EntityFramework和MVC开发,交流QQ群:75272942(2000人超级群) http://nopchina.net/ http://www.rssboard.org/rss-specification BlogEngine.NET 2.9.1.0 zh-cn http://nopchina.net/opml.axd http://www.nopchina.net/syndication.axd wcf nopcommerce中文网 0.000000 0.000000 Asp.net MVC 4 异步方法 <p style="margin-top: 10px; margin-right: auto; margin-left: auto; color: rgb(35, 35, 35); font-family: Verdana, Arial, helvetica, sans-seriff; line-height: 25.2px;">今天我们来看一下,同样功能在&nbsp;<a href="http://www.asp.net/mvc" style="color: rgb(86, 182, 233);">Asp.net MVC 4</a>&nbsp;下的实现,基于.net framework 4.5 下的async支持,让我们的代码更加简单,看下面片断代码名叫Index的Action方法:&nbsp;<br></p><div class="csharpcode" style="font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace;"><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> <span class="kwrd" style="color: rgb(0, 0, 255);">public</span> async Task&lt;ActionResult&gt; IndexAsync()</pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);"> {</pre><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> var cnblogsTask = GetStringAsync(<span class="str" style="color: rgb(0, 96, 128);">"http://www.cnblogs.com"</span>);</pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);"> var myblogTask = GetStringAsync(<span class="str" style="color: rgb(0, 96, 128);">"http://www.cnblogs.com/wintersun"</span>);</pre><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);">&nbsp;</pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);"> <span class="rem" style="color: rgb(0, 128, 0);">// Asynchronously wait for them all to complete.</span></pre><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> await Task.WhenAll(cnblogsTask, myblogTask);</pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);">&nbsp;</pre><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> Operations translations = <span class="kwrd" style="color: rgb(0, 0, 255);">new</span> Operations() { </pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);"> FirstOperation = cnblogsTask.Result, SecondOperation = myblogTask.Result };</pre><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);">&nbsp;</pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);"> <span class="kwrd" style="color: rgb(0, 0, 255);">return</span> View(translations);</pre><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> }</pre></div><p style="margin-top: 10px; margin-right: auto; margin-left: auto; color: rgb(35, 35, 35); font-family: Verdana, Arial, helvetica, sans-seriff; line-height: 25.2px;">&nbsp;</p><div class="csharpcode" style="font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace;"><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> <span class="kwrd" style="color: rgb(0, 0, 255);">private</span> <span class="kwrd" style="color: rgb(0, 0, 255);">static</span> async Task&lt;<span class="kwrd" style="color: rgb(0, 0, 255);">string</span>&gt; GetStringAsync(<span class="kwrd" style="color: rgb(0, 0, 255);">string</span> uri</pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);"> , CancellationToken cancelToken = <span class="kwrd" style="color: rgb(0, 0, 255);">default</span>(CancellationToken))</pre><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> {</pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);"> <span class="kwrd" style="color: rgb(0, 0, 255);">using</span> (HttpClient httpClient = <span class="kwrd" style="color: rgb(0, 0, 255);">new</span> HttpClient())</pre><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> {</pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);"> var response = await httpClient.GetAsync(uri, cancelToken);</pre><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> <span class="kwrd" style="color: rgb(0, 0, 255);">return</span> (await response.Content.ReadAsStringAsync());</pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);"> }</pre><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> }</pre></div><p style="margin-top: 10px; margin-right: auto; margin-left: auto; color: rgb(35, 35, 35); font-family: Verdana, Arial, helvetica, sans-seriff; line-height: 25.2px;"><br>上面的代码我们实现多个Task并行异步,注意<a href="http://msdn.microsoft.com/en-us/library/hh156513(VS.110).aspx" style="color: rgb(86, 182, 233);">async</a>与<a href="http://msdn.microsoft.com/en-us/library/hh156513(VS.110).aspx" style="color: rgb(86, 182, 233);">await</a>关键字,在.NET 4.5 与 Visual Studio 2012下可用。你只可以使用await关键字同时标注了async的方法。使用异法的<a href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclient(VS.110).aspx" style="color: rgb(86, 182, 233);">HttpClient</a>来替代<a href="http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx" style="color: rgb(86, 182, 233);">WebClient</a>类,await关键字运用到<a href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclient(VS.110).aspx" style="color: rgb(86, 182, 233);">HttpClient</a>的异步方法上。使用<a href="http://msdn.microsoft.com/en-us/library/hh194766.aspx" style="color: rgb(86, 182, 233);">Task.WhenAll</a>等待所有任务结束。我们知道TPL中还提供了<a href="http://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken(VS.110).aspx" style="color: rgb(86, 182, 233);">CancellationToken</a>,MVC框架中也提供了<a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.asynctimeoutattribute(VS.108).aspx" style="color: rgb(86, 182, 233);">AsyncTimeout</a>特性,看面下面示例代码:&nbsp;<br><br></p><div class="csharpcode" style="font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace;"><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> [AsyncTimeout(100)]</pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);"> [HandleError(ExceptionType = <span class="kwrd" style="color: rgb(0, 0, 255);">typeof</span>(TimeoutException),View = <span class="str" style="color: rgb(0, 96, 128);">"Error"</span>)]</pre><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> <span class="kwrd" style="color: rgb(0, 0, 255);">public</span> async Task&lt;ActionResult&gt; IndexCancleAsync()</pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);"> {</pre><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> var cancellationToken = <span class="kwrd" style="color: rgb(0, 0, 255);">new</span> CancellationToken(<span class="kwrd" style="color: rgb(0, 0, 255);">false</span>);</pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);"> var cnblogsTask = GetStringAsync(<span class="str" style="color: rgb(0, 96, 128);">"http://www.cnblogs.com"</span>, cancellationToken);</pre><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> var myblogTask = GetStringAsync(<span class="str" style="color: rgb(0, 96, 128);">"http://www.cnblogs.com/wintersun"</span>, cancellationToken);</pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);">&nbsp;</pre><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> await Task.WhenAll(cnblogsTask, myblogTask);</pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);">&nbsp;</pre><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> Operations translations = <span class="kwrd" style="color: rgb(0, 0, 255);">new</span> Operations()</pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);"> {</pre><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> FirstOperation = cnblogsTask.Result,</pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);"> SecondOperation = myblogTask.Result</pre><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> };</pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);">&nbsp;</pre><pre class="alt" style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; width: 1261px; background-color: rgb(244, 244, 244);"> <span class="kwrd" style="color: rgb(0, 0, 255);">return</span> View(translations);</pre><pre style="margin: 0em; font-size: small; color: black; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);"> }</pre></div><p style="margin-top: 10px; margin-right: auto; margin-left: auto; color: rgb(35, 35, 35); font-family: Verdana, Arial, helvetica, sans-seriff; line-height: 25.2px;"><br>我们设置异步超时为100毫秒,并且我们可以传递<a href="http://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken(VS.110).aspx" style="color: rgb(86, 182, 233);">CancellationToken</a>的参数给具体的Task。 最后把结果返回给View,实际你可以自行Debug看其中过程。</p><p style="margin-top: 10px; margin-right: auto; margin-left: auto; color: rgb(35, 35, 35); font-family: Verdana, Arial, helvetica, sans-seriff; line-height: 25.2px;">希望对您Web开发有帮助。&nbsp;</p> http://nopchina.net/post/aspnet-mvc4-async.html wucf2004@gmail.com http://nopchina.net/post/aspnet-mvc4-async.html#disqus_thread http://nopchina.net/post.aspx?id=361cdc5b-6c90-482f-bf97-f0f468073075 Sun, 14 Feb 2016 23:02:00 +0800 MVC Admin http://nopchina.net/pingback.axd http://nopchina.net/post.aspx?id=361cdc5b-6c90-482f-bf97-f0f468073075 0 http://nopchina.net/trackback.axd?id=361cdc5b-6c90-482f-bf97-f0f468073075 http://nopchina.net/post/aspnet-mvc4-async.html#disqus_thread http://nopchina.net/syndication.axd?post=361cdc5b-6c90-482f-bf97-f0f468073075 分享一个模拟提交带文件的表单的方法 public string PostFormData(List&lt;FormItem&gt; list, string uri)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //请求 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WebRequest req = WebRequest.Create(uri);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; req.Method = "POST";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; req.ContentType = "multipart/form-data; boundary=" + boundary;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //组织表单数据 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StringBuilder sb = new StringBuilder();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach (FormItem item in list)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; switch (item.ParamType)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case ParamType.Text:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.Append("--" + boundary);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.Append("\r\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.Append("Content-Disposition: form-data; name=\"" + item.Name + "\"");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.Append("\r\n\r\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.Append(item.Value);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.Append("\r\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case ParamType.File:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.Append("--" + boundary);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.Append("\r\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.Append("Content-Disposition: form-data; name=\"" + item.Name + "\"; filename=\"" + item.Value + "\"");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.Append("\r\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.Append("Content-Type: application/octet-stream");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.Append("\r\n\r\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string head = sb.ToString();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //post字节总长度<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long length = 0;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] form_data = Encoding.UTF8.GetBytes(head);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //结尾 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] foot_data = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; List&lt;FormItem&gt; fileList = list.Where(f =&gt; f.ParamType == ParamType.File).ToList();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; length = form_data.Length + foot_data.Length;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach (FormItem fi in fileList)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FileStream fileStream = new FileStream(fi.Value, FileMode.Open, FileAccess.Read);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; length += fileStream.Length;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fileStream.Close();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; req.ContentLength = length;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Stream requestStream = req.GetRequestStream();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //发送表单参数 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; requestStream.Write(form_data, 0, form_data.Length);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach (FormItem fd in fileList)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FileStream fileStream = new FileStream(fd.Value, FileMode.Open, FileAccess.Read);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //文件内容 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int bytesRead = 0;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; requestStream.Write(buffer, 0, bytesRead);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //结尾 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; requestStream.Write(foot_data, 0, foot_data.Length);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; requestStream.Close();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //响应 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WebResponse pos = req.GetResponse();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StreamReader sr = new StreamReader(pos.GetResponseStream(), Encoding.UTF8);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string html = sr.ReadToEnd().Trim();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sr.Close();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (pos != null)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pos.Close();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pos = null;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (req != null)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; req = null;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return html;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public string SaveFileFromUrl(string fileName, string url)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WebResponse response = null;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Stream stream = null;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; response = request.GetResponse();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stream = response.GetResponseStream();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!response.ContentType.ToLower().StartsWith("text/"))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SaveBinaryFile(response, fileName);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return sr.ReadToEnd();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch (Exception err)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return err.ToString();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return "complete";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public bool SaveBinaryFile(WebResponse response, string fileName)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool value = true;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] buffer = new byte[1024];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (System.IO.File.Exists(fileName))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.IO.File.Delete(fileName);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Stream outStream = System.IO.File.Create(fileName);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Stream inStream = response.GetResponseStream();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int l;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; l = inStream.Read(buffer, 0, buffer.Length);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (l &gt; 0)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; outStream.Write(buffer, 0, l);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (l &gt; 0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; outStream.Close();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inStream.Close();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value = false;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return value;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public class FormItem<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public string Name { get; set; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public ParamType ParamType { get; set; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public string Value { get; set; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public enum ParamType<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; File<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } http://nopchina.net/post/post-form-data-with-images.html wucf2004@gmail.com http://nopchina.net/post/post-form-data-with-images.html#disqus_thread http://nopchina.net/post.aspx?id=b4ca00b9-2cda-4a6f-ac09-26261540f121 Wed, 20 Jan 2016 14:01:00 +0800 解决方案 Admin http://nopchina.net/pingback.axd http://nopchina.net/post.aspx?id=b4ca00b9-2cda-4a6f-ac09-26261540f121 0 http://nopchina.net/trackback.axd?id=b4ca00b9-2cda-4a6f-ac09-26261540f121 http://nopchina.net/post/post-form-data-with-images.html#disqus_thread http://nopchina.net/syndication.axd?post=b4ca00b9-2cda-4a6f-ac09-26261540f121 nopcommerce中文网 | Error

nopcommerce中文网

nopcommerce是国外asp.net领域一个高质量的b2c开源项目,基于EntityFramework和MVC开发,交流QQ群:75272942(2000人超级群)

导航 - 搜索

Ooops! An unexpected error has occurred.

This one's down to me! Please accept my apologies for this - I'll see to it that the developer responsible for this happening is given 20 lashes (but only after he or she has fixed this problem).