WP8上传文件

时间:2015-01-30 14:55:10

标签: c# silverlight windows-phone-8

我正在尝试通过C#Windows Phone 8.1应用程序上的网站API上传文件。我想使用Windows.Web.Http lib。我可以选择一个带有FilePicker的文件(fileChoosenFromFilePicker var)。

错误 我每次都收到一个http 400(错误请求)错误。

问题 有人能帮我吗 ? 我怎样才能看到我的代码生成的POST请求?我可以在服务器端进行数据包捕获,但我希望在客户端看到。

代码

在API文档中,有一个例子:

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

这是我的代码:

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

        }

修改

我已经使用了你的代码。并做了一些改变:

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

        }

现在好多了。但是,它仍然无法正常工作。

在这里你可以找到POST结果:http://requestb.in/p5jvlfp5?inspect#13hh5o

如果我使用curl,我会得到以下结果:http://requestb.in/pwng8npw?inspect

在我的版本中,在POST结果中,没有&#34; Content-Type:application / octet-stream&#34;。我怎么指定这个?或者可能是HttpStreamContent问题?

1 个答案:

答案 0 :(得分:1)

据我所知,你不是在一起撰写任何这些信息。当您进入实际的PostAsync()调用时,您将沿着一个完全空的HttpMultipartContent类型的包发送。你对变量requestUpload进行了维度,然后除了发布它之外什么也不做任何事情。

尝试通过创建一个外部HttpMultipartFormDataContent对象来编写请求,您将添加HttpStreamContent和HttpFormUrlEncodedContent对象,如下所示。我尝试重新创建,我可以确定服务器在Seafile的python上传方法文档的请求中期望。

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
}