Home >Backend Development >PHP Tutorial >How to Successfully Upload Files and Pass Additional Parameters with HTTP POST Multipart/Form-Data in Windows Phone 8?
When developing Windows Phone 8 applications, it's common to need to upload files to a server. This can be achieved using HTTP POST with the multipart/form-data MIME type. However, there are specific considerations for Windows Phone 8.
The provided code sample attempts to upload a file and pass a string parameter ("userid=SOME_ID") using HTTP POST multipart/form-data. However, it encounters an issue where the file is not being uploaded successfully.
The specific issue lies in the GetRequestStreamCallback method. The request stream is obtained using request.EndGetRequestStream(asynchronousResult), but the "userid=some_user_id" parameter is not being added to the request.
To pass additional parameters like "userid", it's necessary to create a boundary for the multipart/form-data request. This boundary separates the different parts of the request (file and additional parameters).
Here's an improved code sample that properly handles multipart/form-data requests and passes additional parameters:
private void HttpPost(byte[] fileBytes, string additionalParam) { HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.myserver.com/upload.php"); httpWebRequest.ContentType = "multipart/form-data; boundary=---------------------------" + DateTime.Now.Ticks.ToString("x"); httpWebRequest.Method = "POST"; var asyncResult = httpWebRequest.BeginGetRequestStream((ar) => { GetRequestStreamCallback(ar, fileBytes, additionalParam); }, httpWebRequest); } private void GetRequestStreamCallback(IAsyncResult asynchronousResult, byte[] postData, string additionalParam) { HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; Stream postStream = request.EndGetRequestStream(asynchronousResult); // Create boundary start string string boundaryStart = "--" + request.ContentType.Substring(request.ContentType.IndexOf('=') + 1); // Write file content string fileBoundary = boundaryStart + Environment.NewLine + "Content-Disposition: form-data; name=\"file\"; filename=\"myfile.db\"" + Environment.NewLine + "Content-Type: application/octet-stream" + Environment.NewLine + Environment.NewLine; postStream.Write(Encoding.UTF8.GetBytes(fileBoundary), 0, fileBoundary.Length); postStream.Write(postData, 0, postData.Length); // Write additional parameter string paramBoundary = Environment.NewLine + boundaryStart + Environment.NewLine + "Content-Disposition: form-data; name=\"userid\"" + Environment.NewLine + Environment.NewLine + additionalParam + Environment.NewLine; postStream.Write(Encoding.UTF8.GetBytes(paramBoundary), 0, paramBoundary.Length); // Write boundary end string string boundaryEnd = Environment.NewLine + "--" + request.ContentType.Substring(request.ContentType.IndexOf('=') + 1) + "--" + Environment.NewLine; postStream.Write(Encoding.UTF8.GetBytes(boundaryEnd), 0, boundaryEnd.Length); postStream.Close(); var asyncResult = request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request); }
By following these guidelines, you can successfully upload files to a server and pass additional parameters using HTTP POST multipart/form-data in Windows Phone 8 applications.
The above is the detailed content of How to Successfully Upload Files and Pass Additional Parameters with HTTP POST Multipart/Form-Data in Windows Phone 8?. For more information, please follow other related articles on the PHP Chinese website!