当前位置:编程学习 > wap >>

为什么使用post上传不了文件?总是异常

 private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Method = "POST";
            webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
            webRequest.BeginGetRequestStream(HttpPostCallback, webRequest);
            stringDict.Clear();
            stringDict.Add("user_id", textbox1.Text);
        }

        private void HttpPostCallback(IAsyncResult result)
        {
            StringBuilder request = new StringBuilder();
            var req = result.AsyncState as HttpWebRequest;
            var requestStream = req.EndGetRequestStream(result);
            var beginBoundary = "--" + boundary + "\r\n";  
            var stringKeyHeader = "\r\n--" + boundary +
                                  "\r\nContent-Disposition: form-data; name=\"{0}\"" +
                                  "\r\n\r\n{1}\r\n";
           // var endBoundary = "--" + boundary + "--\r\n";
            byte[] endBoundary = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");  
            /*foreach (string key in stringDict.Keys)
            {
                string s = string.Format(stringKeyHeader, key, stringDict[key]);
                request.Append(s);
            }*/

            string fileKeyName = "imgfile";  
            /*const string filePartHeader =  
                "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n"+  
                         "Content-Type: application/octet-stream\r\n\r\n";*/
            /*const string filePartHeader =
                "Content-Disposition: attachment; name=\"{0}\"; filename=\"{1}\"\r\n" +
                         "Content-Type:image/jpg\r\nContent-Transfer-Encoding: binary\r\n\r\n";*/
            const string filePartHeader =
               "Content-Disposition:form-data;name=\"{0}\";filename=\"{1}\"\r\n" +
                        "Content-Type:image/jpg\r\n\r\n";
            var header = string.Format(filePartHeader, fileKeyName, filePath);
            request.Append(beginBoundary);
            request.Append(header);
            //request.Append("Content-Type:image/jpg");
            byte[] requestbytes = Encoding.UTF8.GetBytes(request.ToString());
            requestStream.Write(requestbytes,0,requestbytes.Length);
            requestStream.Flush();
            this.Dispatcher.BeginInvoke(() =>
            {
                textBlock1.Text = request.ToString();
            });
            byte[] transfer = null;
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(filePath, System.IO.FileMode.Open,isf);
                //var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);  
                transfer = new byte[fileStream.Length];
                int transferRead;
                if((transferRead = fileStream.Read(transfer, 0, transfer.Length)) != 0)
                {
                    requestStream.Write(transfer, 0, transfer.Length);
                    requestStream.Flush();
                }
               /* this.Dispatcher.BeginInvoke(() =>
              {
                textBlock1.Text =transferRead.ToString();
                });*/
            }
            
            requestStream.Write(endBoundary, 0, endBoundary.Length);
            /*this.Dispatcher.BeginInvoke(() =>
            {
                textBlock1.Text =endBoundary.Length.ToString();
            });*/
            requestStream.Flush();
            requestStream.Close();
            req.BeginGetResponse(HttpGetResponseCallback, req);
        }

        private void HttpGetResponseCallback(IAsyncResult result)
        {
            var req = result.AsyncState as HttpWebRequest;
            var resp = req.EndGetResponse(result);
            var strm = resp.GetResponseStream();
            var reader = new StreamReader(strm);
            string str1 = reader.ReadToEnd();
            char[] str2 = new char[str1.Length];

            for (int i = 0, j = 0; i < str1.Length - 1; i++, j++)
            {
                if (str1[i] == '\\' && str1[i + 1] == 'u')
                {
                    str2[j] = (char)Convert.ToInt32(str1.Substring(i + 2, 4), 16);
                    i = i + 5;
                }
                else
                {
                    str2[j] = str1[i];
                }

            }
            string str3 = new string(str2);
            this.Dispatcher.BeginInvoke(() =>
            {
                textBlock1.Text = str3;
            });
        }
--------------------编程问答-------------------- 代码有bug --------------------编程问答-------------------- 报什么异常,异常信息贴出来。 --------------------编程问答-------------------- 看下这个类, http://www.silverlightchina.net/html/zhuantixilie/winphone7/2011/0419/7026.html --------------------编程问答-------------------- 代码有问题。。。。。。 --------------------编程问答--------------------  跟踪一下啊,,,这么长的代码,看都不想看了,问问题最好能简短说明问题,最好,
补充:移动开发 ,  Windows Phone
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,