Keval Langalia
Keval Langalia

Reputation: 1892

BackgroundTransferService with POST method and Parameters

I want to upload a file (VideoFile) to server through BackgroundTransferService.

My problem is, I also want to send 2 parameters along with File (POST request).

So, is it possible to send parameters along with File upload while using BackgroundTransferService API..?

Code with 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);
        }

Please ask if anyone wants more info and unable to understand my question.

I want a quick response. Yes or No.. and if Yes then How..?

Upvotes: 6

Views: 333

Answers (2)

HouseCat
HouseCat

Reputation: 1623

I am not 100% sure on what you are trying to do. However, I believe you can via HTTP Headers.

BackgroundTransferRequest.Headers Property
https://msdn.microsoft.com/en-us/library/windows/apps/microsoft.phone.backgroundtransfer.backgroundtransferrequest.headers(v=vs.105).aspx

And as the sender with the Tag Property.
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.

Upvotes: 1

user4781375
user4781375

Reputation:

i ran into similar kind of problem before few weeks. i somehow managed this file upload by HttpClient.

Check code

        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);

Upvotes: 2

Related Questions