6

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..?

Keval Langalia
  • 1,762
  • 1
  • 16
  • 29

2 Answers2

2

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);
  • thanks for your reply but i want to upload file in `Background` so i guess i have to use `BackgroundTransferService`. – Keval Langalia May 05 '15 at 08:56
  • I would recommend to mark your methods as `async` and simply call this method to start the background process. No need for `BackgroundTransferService` anymore. – Florian Moser May 09 '15 at 16:23
  • @FlorianMoser Thanks for your inputs, but the reason i have to use `BackgroundTransferService` is i also want to track how much bytes have been uploaded and how much remains. These stats can be tracked only if we use BackgroundTransferService, not through `HttpClient`. Is there any other alternative..? – Keval Langalia May 10 '15 at 05:22
  • 1
    It seems like HttpClient can do this too: http://stackoverflow.com/questions/21169573/how-to-implement-progress-reporting-for-portable-httpclient – Florian Moser May 10 '15 at 06:54
  • sounds great..! I'm trying to apply it on my case.. let's see if it works.. I'll mention here soon..! – Keval Langalia May 10 '15 at 08:44
  • sadly, I've to use `HttpClient` and complete the upload with POST request (with few limitation). @FlorianMoser thanks for your reference links. that helped me solve my other problem. @learner thanks for suggesting alternative. – Keval Langalia May 15 '15 at 05:13
1

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.

HouseCat
  • 1,559
  • 20
  • 22
  • I've tried that already..! but nop, that's not working... it gives `Error 400 Bad Request` and completes unsuccessfully. Thanks for your inputs anyway.. – Keval Langalia May 14 '15 at 08:53