具有 POST 方法和参数的 BackgroundTransferService
BackgroundTransferService with POST method and Parameters
我想上传文件 (VideoFile) 到服务器通过 BackgroundTransferService
.
我的问题是,我还想发送 2 个参数 以及文件(POST 请求)。
那么,在使用 BackgroundTransferService
API..?
时,是否可以在文件上传的同时发送参数?
代码 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);
}
请询问是否有人需要更多信息但无法理解我的问题。
我想要快速回复。是或否..如果是那么如何..?
我 运行 几周前遇到了类似的问题。我以某种方式管理了 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 Headers.
BackgroundTransferRequest.Headers 属性
https://msdn.microsoft.com/en-us/library/windows/apps/microsoft.phone.backgroundtransfer.backgroundtransferrequest.headers(v=vs.105).aspx
并且作为带有标签 属性 的发件人。
https://msdn.microsoft.com/en-us/library/windows/apps/microsoft.phone.backgroundtransfer.backgroundtransferrequest.tag(v=vs.105).aspx
This property can be used to associate custom data associated with a
transfer. The application can set the value when the transfer request
is created. When the transfer request is retrieved, using the Requests
property or the Find(String) method, the Tag property will contain the
previously set data. This property is only used by the calling
application and is ignored by the system. The maximum length of this
property is 4000 characters, but it is recommended that you keep the
size of the data smaller in order to improve performance.
我想上传文件 (VideoFile) 到服务器通过 BackgroundTransferService
.
我的问题是,我还想发送 2 个参数 以及文件(POST 请求)。
那么,在使用 BackgroundTransferService
API..?
代码 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);
}
请询问是否有人需要更多信息但无法理解我的问题。
我想要快速回复。是或否..如果是那么如何..?
我 运行 几周前遇到了类似的问题。我以某种方式管理了 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 Headers.
BackgroundTransferRequest.Headers 属性
https://msdn.microsoft.com/en-us/library/windows/apps/microsoft.phone.backgroundtransfer.backgroundtransferrequest.headers(v=vs.105).aspx
并且作为带有标签 属性 的发件人。
https://msdn.microsoft.com/en-us/library/windows/apps/microsoft.phone.backgroundtransfer.backgroundtransferrequest.tag(v=vs.105).aspx
This property can be used to associate custom data associated with a transfer. The application can set the value when the transfer request is created. When the transfer request is retrieved, using the Requests property or the Find(String) method, the Tag property will contain the previously set data. This property is only used by the calling application and is ignored by the system. The maximum length of this property is 4000 characters, but it is recommended that you keep the size of the data smaller in order to improve performance.