奇怪的上传文件“空错误”
最近想写一个WP7上的文件上传软件,但是遇到了问题,主要在于异步上传后EndGetResponse时候出错,错误类型很诡异,错误描述是空
WP7代码如下
public class upTest { RequestInfo m_requestInfo = new RequestInfo(); IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); public upTest() { } public void start() { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://192.168.1.118/ajax/uptest.ashx")) as HttpWebRequest; request.Method = "POST"; IAsyncResult asyncResult = request.BeginGetRequestStream(new AsyncCallback(RequestStreamCallBack), request); } public void Start(string fileName) { string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x"); byte[] boundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + strBoundary + "\r\n"); //请求头部信息 StringBuilder sb = new StringBuilder(); sb.Append("--"); sb.Append(strBoundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\""); sb.Append("file"); sb.Append("\"; filename=\""); sb.Append(System.IO.Path.GetFileName(fileName)); 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(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://192.168.1.118/ajax/uptest.ashx")); request.Method = "POST"; m_requestInfo.Boundary = strBoundary; m_requestInfo.FullName = fileName; m_requestInfo.PostHeader = strPostHeader; IAsyncResult asyncResult = request.BeginGetRequestStream(new AsyncCallback(RequestStreamCallBack), request); } private void RequestStreamCallBack(IAsyncResult ar) { HttpWebRequest request = ar.AsyncState as HttpWebRequest; Stream requestStream = request.EndGetRequestStream(ar); request.ContentType = "multipart/form-data; boundary=" + m_requestInfo.Boundary; byte[] postHeaderBytes = Encoding.UTF8.GetBytes(m_requestInfo.PostHeader); byte[] boundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + m_requestInfo.Boundary + "\r\n"); IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(m_requestInfo.FullName, FileMode.Open, isf); // Write the post header requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); // Stream the file contents in small pieces (4096 bytes, max). StreamWriter sw = new StreamWriter(requestStream); byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)isfs.Length))]; int bytesRead = 0; while ((bytesRead = isfs.Read(buffer, 0, buffer.Length)) != 0) requestStream.Write(buffer, 0, bytesRead); // Add the trailing boundary requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); isfs.Close(); request.BeginGetResponse(new AsyncCallback(ResponseCallBack), request); } private void ResponseCallBack(IAsyncResult ar) { HttpWebRequest request = ar.AsyncState as HttpWebRequest; WebResponse response = null; try { response = request.EndGetResponse(ar); StreamReader responseReader = new StreamReader(response.GetResponseStream()); string strResponse = responseReader.ReadToEnd(); } catch (Exception ex) { string strErr = ex.Message; } /*Dispatcher.BeginInvoke(() => { textBox1.Text = responseReader.ReadToEnd(); });*/ } }
public void ProcessRequest (HttpContext context) { string UpLoadPath; StringBuilder sb = new StringBuilder(); UpLoadPath = HttpContext.Current.Server.MapPath("..\\") + "\\images"; if (!Directory.Exists(UpLoadPath)) { Directory.CreateDirectory(UpLoadPath); } HttpContext.Current.Response.Write("End Here"); return; if (HttpContext.Current.Request.Files.Count > 0) { HttpPostedFile hpf = HttpContext.Current.Request.Files[0]; string tmpFile; tmpFile = UpLoadPath + "\\" + hpf.FileName; if (!File.Exists(tmpFile)) { File.Delete(tmpFile); } hpf.SaveAs(tmpFile);//保存文件 String unicodestring = "success:" + hpf.FileName; HttpContext.Current.Response.Write(unicodestring); } else { HttpContext.Current.Response.Write("No Files"); } } public bool IsReusable { get { return false; } }