Kev*_*lia 6 c# file-upload windows-phone background-transfer windows-phone-8
我想上传一个文件(录像档案)到服务器通过 BackgroundTransferService.
我的问题是,我还想发送2个参数和文件(POST请求).
那么,使用BackgroundTransferServiceAPI时是否可以发送参数和文件上传..?
代码BackgroundTransferService:
BackgroundTransferRequest req = new BackgroundTransferRequest(new Uri("ServerURL", UriKind.Absolute));
req.Method = "POST";
req.TransferPreferences = TransferPreferences.AllowCellularAndBattery;
string uploadLocationPath = "/Shared/Transfers/myVideoFile.mp4";
string downloadLocationPath = "/Shared/Transfers/response.txt";
req.UploadLocation = new Uri(uploadLocationPath, UriKind.Relative);
req.DownloadLocation = new Uri(downloadLocationPath, UriKind.Relative);
req.TransferProgressChanged += req_TransferProgressChanged;
req.TransferStatusChanged += req_TransferStatusChanged;
try
{
BackgroundTransferService.Add(req);
}
catch (Exception ex)
{
MessageBox.Show("Unable to add video to upload queue.\nPlease try again later.", App.appName, MessageBoxButton.OK);
}
Run Code Online (Sandbox Code Playgroud)
请询问是否有人想要更多信息并且无法理解我的问题.
我想快速回复.是或否......如果是,则如何......?
小智 2
几周前我遇到了类似的问题。我以某种方式管理了这个文件上传HttpClient。
检查代码
HttpClient client = new HttpClient();
StorageFile file = null; // assign your file here
MultipartFormDataContent formdata = new MultipartFormDataContent();
formdata.Add(new StringContent("value"), "key");
formdata.Add(new StreamContent(await file.OpenStreamForReadAsync()), "file", "recordedVideoFile2.mp4");
var response = await client.PostAsync(new Uri("URL here"), formdata);
Run Code Online (Sandbox Code Playgroud)