Home > Backend Development > PHP Tutorial > How to Upload Files to a Server using HTTP POST Multipart/Form-Data in Windows Phone 8?

How to Upload Files to a Server using HTTP POST Multipart/Form-Data in Windows Phone 8?

Barbara Streisand
Release: 2024-12-14 09:51:15
Original
547 people have browsed it

How to Upload Files to a Server using HTTP POST Multipart/Form-Data in Windows Phone 8?

Uploading Files to Server using HTTP POST Multipart/Form-Data in Windows Phone 8

In Windows Phone 8, uploading files to a server via HTTP POST multipart/form-data involves using the HttpWebRequest class. Here's how you can implement it:

async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(DBNAME);
    byte[] fileBytes = null;
    using (var stream = await file.OpenReadAsync())
    {
        fileBytes = new byte[stream.Size];
        using (var reader = new DataReader(stream))
        {
            await reader.LoadAsync((uint)stream.Size);
            reader.ReadBytes(fileBytes);
        }
    }
    
    // Construct HTTP request with multipart/form-data content type and userID data
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.myserver.com/upload.php");
    httpWebRequest.ContentType = "multipart/form-data";
    var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
    httpWebRequest.Method = "POST";
    
    // Begin obtaining request stream for writing file bytes and userID data
    var asyncResult = httpWebRequest.BeginGetRequestStream((ar) => { GetRequestStreamCallback(ar, fileBytes, boundary); }, httpWebRequest); 
}

private void GetRequestStreamCallback(IAsyncResult asynchronousResult, byte[] postData, string boundary)  
{
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;  
    Stream postStream = request.EndGetRequestStream(asynchronousResult);  
    
    // Write file bytes to request stream
    postStream.Write(postData, 0, postData.Length);  
    
    // Write userID data to request stream
    var lineBreak = "\r\n";
    var formData = $"--{boundary}{lineBreak}Content-Disposition: form-data; name=\"userid\"{lineBreak}{lineBreak}SOME_ID{lineBreak}";
    var formDataBytes = System.Text.Encoding.UTF8.GetBytes(formData);
    postStream.Write(formDataBytes, 0, formDataBytes.Length);
    
    // Closing boundary string
    var closingBoundary = $"--{boundary}--{lineBreak}";
    var closingBoundaryBytes = System.Text.Encoding.UTF8.GetBytes(closingBoundary);
    postStream.Write(closingBoundaryBytes, 0, closingBoundaryBytes.Length);  
    
    postStream.Close();  
    var asyncResult = request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);  
}  

private void GetResponseCallback(IAsyncResult asynchronousResult)  
{  
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;  
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);  
    // ... Handle response
}  
Copy after login

The above is the detailed content of How to Upload Files to a Server using HTTP POST Multipart/Form-Data in Windows Phone 8?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template