C# 使用POST方法和参数的BackgroundTransferService
我想通过C# 使用POST方法和参数的BackgroundTransferService,c#,windows-phone-8,file-upload,windows-phone,background-transfer,C#,Windows Phone 8,File Upload,Windows Phone,Background Transfer,我想通过后台传输服务将文件(视频文件)上传到服务器 我的问题是,我还想随文件一起发送两个参数(POST请求) 那么,在使用BackgroundTransferServiceAPI..时,是否可以将参数与文件上载一起发送 使用后台传输服务编码: BackgroundTransferRequest req = new BackgroundTransferRequest(new Uri("ServerURL", UriKind.Absolute)); req.Metho
后台传输服务
将文件(视频文件)上传到服务器
我的问题是,我还想随文件一起发送两个参数(POST请求)
那么,在使用BackgroundTransferService
API..时,是否可以将参数与文件上载一起发送
使用后台传输服务编码
:
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);
}
请询问是否有人想了解更多信息,但无法理解我的问题
我想要一个快速的回应。是或否。。如果是,那怎么办?几周前,我遇到了类似的问题。我通过
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);
我不是100%确定你想做什么。但是,我相信您可以通过HTTP头 BackgroundTransferRequest.Headers属性
并作为具有标记属性的发件人。
此属性可用于将自定义数据与 转移应用程序可以在传输请求时设置值 是创建的。检索传输请求时,使用 属性或Find(String)方法,则标记属性将包含 以前设置的数据。此属性仅由调用方使用 应用程序和被系统忽略。此文件的最大长度 属性为4000个字符,但建议您保留 为了提高性能,数据的大小变小了
感谢您的回复,但我想在
后台上传文件
,因此我想我必须使用后台传输服务
。我建议将您的方法标记为异步
,并简单地调用此方法来启动后台进程。不再需要BackgroundTransferService
。@FlorianMoser感谢您的输入,但我必须使用BackgroundTransferService
的原因是我还想跟踪已上载的字节数和剩余的字节数。只有使用BackgroundTransferService,而不是通过HttpClient
,才能跟踪这些统计数据。还有其他选择吗?似乎HttpClient也能做到:听起来很棒。。!我想把它应用到我的案子上。。让我们看看它是否有效。。我很快就会在这里提到。。!我已经试过了。。!但是不,那不起作用。。。它发出错误400错误请求
,但未成功完成。无论如何,谢谢你的意见。。