BackgroundTransferService

BackgroundTransferService

我想通过 BackgroundTransferService将文件(VideoFile)上传到服务器

我的问题是,我还想与文件(POST请求)一起发送2个参数

因此,在使用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);

关于c# - 带有POST方法和参数的BackgroundTransferService,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30048147/

10-13 05:59