简体   繁体   中英

WP8 Upload file

I am trying to upload a file trough a web site API on my C# Windows Phone 8.1 application. I want to use Windows.Web.Http lib. I can choose a file with an FilePicker (fileChoosenFromFilePicker var).

Error I got a http 400 (Bad request) error everytime.

Question Can someone help me ? How I can see the POST request generate by my code ? I can make a packet capture on the server side, but I want to see on the client side.

Code

In the API documentation, there is an example :

curl -H "Authorization: Token f2210dacd9c6ccb8133606d94ff8e61d99b477fd" -F file=@test.txt -F filename=test.txt -F parent_dir=/ http://cloud.seafile.com:8082/upload-api/ef881b22

And this is my code :

 var HttpClientUpload = new HttpClient();
 HttpMultipartContent requestUpload = new HttpMultipartContent();
 HttpMultipartFormDataContent requestUploadContent = new HttpMultipartFormDataContent();

var fileContent = await fileChoosenFromFilePicker.OpenReadAsync();

        HttpFormUrlEncodedContent requestUploadData = new HttpFormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("filename", fileChoosenFromFilePicker.Name),
                    new KeyValuePair<string, string>("parent_dir", "/")
                });

        HttpClientUpload.DefaultRequestHeaders.Add("Authorization", "token " + authorization);

         IInputStream inputStream = await fileChoosenFromFilePicker.OpenAsync(FileAccessMode.Read);
        requestUploadContent.Add(new HttpStreamContent(inputStream), "myFile", fi.Name);

           try
        {
            HttpResponseMessage response = await HttpClientUpload.PostAsync(uristringForUpload, requestUpload);
            response.EnsureSuccessStatusCode();
        }
        catch (Exception ex)
        {

        }

Edit :

I have use your code. and made some change :

var HttpClientUpload = new HttpClient(filter);
        HttpMultipartFormDataContent requestUploadContent = new HttpMultipartFormDataContent();

        var fileContent = await fileChoosenFromFilePicker.OpenReadAsync();

        HttpClientUpload.DefaultRequestHeaders.Add("Authorization", "token " + authorization);

        FileInfo fi = new FileInfo(fileChoosenFromFilePicker.Path);
        string fileName = fi.Name;         

        var inputStream = await fileChoosenFromFilePicker.OpenAsync(FileAccessMode.Read);
        requestUploadContent.Add(new HttpStreamContent(inputStream), "myFile", fi.Name);

        var values = new[]
                {
                    new KeyValuePair<string, string>("filename", fileName),
                    new KeyValuePair<string, string>("parent_dir", "/")
                };

        foreach (var keyValuePair in values)
        {
            requestUploadContent.Add(new HttpStringContent(keyValuePair.Value), keyValuePair.Key);
        }


        try
        {
            HttpResponseMessage responseLogin = await HttpClientUpload.PostAsync(uristringForUpload, requestUploadContent);
            responseLogin.EnsureSuccessStatusCode();

        }
        catch (Exception ex)
        {

        }

It's better now. But, it still not working.

Here you cand find the POST result : http://requestb.in/p5jvlfp5?inspect#13hh5o

If I made with curl I have this result : http://requestb.in/pwng8npw?inspect

On my version, in the POST result, there is no "Content-Type: application/octet-stream". How I can specify this ? Or it's maybe with the HttpStreamContent problem ?

As far as I can tell, you are not composing any of this information together. By the time you get to the actual PostAsync() call, you're sending along a completely empty package of type HttpMultipartContent. You dimension the variable requestUpload and then never do anything at all with it other than post it.

Try composing the request by creating an outer HttpMultipartFormDataContent object to which you will add HttpStreamContent and HttpFormUrlEncodedContent objects as below. I've tried to recreate as best I can determine the server is expecting in the request from Seafile's documentation of their python upload method.

var client = new HttpClient();

var request = new HttpMultipartFormDataContent();
request.Headers.Add("Authorization", "Token " + authorization);

// Add file content request part
var fileContent = await fileChosenFromFilePicker.OpenAsync(FileAccessMode.Read);
var requestContent = new HttpStreamContent(fileContent);
requestContent.Headers.Add("Content-Type", "application/octet-stream");
request.Add(requestContent, "file", fileChosenFromFilePicker.Name);

// Add form data request part
request.Add(new HttpFormUrlEncodedContent(new Dictionary<string, string>
{
    {"parent_dir", "/"}
}));

try
{
    // Submit multipart request object
    var response = await client.PostAsync(new Uri(uriStringForUpload), request);
    response.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
    // TODO
}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM