首頁 > 後端開發 > php教程 > 如何在 Windows Phone 8 中使用 HTTP POST Multipart/Form-Data 將檔案上傳到伺服器?

如何在 Windows Phone 8 中使用 HTTP POST Multipart/Form-Data 將檔案上傳到伺服器?

Barbara Streisand
發布: 2024-12-14 09:51:15
原創
547 人瀏覽過

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

在Windows Phone 8 中使用HTTP POST Multipart/Form-Data 將檔案上傳到伺服器

在Windows Phone 8 中,透過HTTP POST multipart/form 將檔案上傳到伺服器-data 涉及使用HttpWebRequest 類別。實作方法如下:

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
}  
登入後複製

以上是如何在 Windows Phone 8 中使用 HTTP POST Multipart/Form-Data 將檔案上傳到伺服器?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板