Search code examples
c#windows-phone-8file-uploadwindows-phonebackground-transfer

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


Solution

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