无法上传文件名有特殊字符谷歌驱动REST [英] Can't upload filename with special character to google drive REST

查看:430
本文介绍了无法上传文件名有特殊字符谷歌驱动REST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的错误,当我尝试将文件上传到Google云端硬盘API与我的Web应用程序和文件有口音的字符,例如像C。

I'm getting error when I try to upload a file to google drive api with my web application and the file has an character with accent, like for example 'ç'.

我上传了两个请求,也有文件信息并返回URI到该文件应该被上传的初步请求,并以文件本身的后续请求的文件。

I'm uploading the file with two requests, an initial request that has the file info and return the uri to where the file should be uploaded, and the subsequent request with the file itself.

在第一个请求出现错误(一个与文件信息)。

The error occurs in the first request (the one with file information).

返回的错误仅仅是一个'400 - 错误的请求,我没有idead如何上传文件用这种characteres的

The error returned is just an '400 - bad request', and I have no idead how to upload the file with this kind of characteres.

下面是我的应用程序生成的,当我尝试上传gif图片文件请求的例子。

Here is an example of request generated by my application when I try to upload an gif image file.

Content-Type: application/json; charset=UTF-8
X-Upload-Content-Length: 257
Authorization: Bearer ******
X-Upload-Content-Type: image/gif
Host: www.googleapis.com
Content-Length: 126
Expect: 100-continue

请求正文只包含文件数据的JSON对象,继承人的code例子,这个初始请求配置:

The request body contains only an json object with file data, heres the code example where this initial request is configured:

private WebRequest CreateInitialUploadRequest(String parentId, String title, String mimeType)
{
    String uri = "https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&access_token=" + AUTH_KEY;

    System.Net.WebRequest request = this.WebRequestFactory.CreateWebRequest(uri);

    request.Method = "POST";
    request.ContentType = "application/json; charset=UTF-8";            
    request.Headers.Add("X-Upload-Content-Length", length.ToString());
    request.Headers.Add("Authorization", string.Format("Bearer {0}", AUTH_KEY));
    request.Headers.Add("X-Upload-Content-Type", mimeType);

    //Setup parent id folder for file
    object[] parents = {new {id = parentId}};        

    object jsonObject = new
    {
        title = title,
        mimeType = mimeType,
        description = "foo bar",
        parents = parents
    };

    String strData = new JavaScriptSerializer().Serialize(jsonObject);

    byte[] data = Encoding.UTF8.GetBytes(strData);

    request.ContentLength = data.Length;

    var stream = request.GetRequestStream();
    stream.Write(data, 0, data.Length);
    stream.Close();

    return request;
}

当我尝试调用的方法是这样的:

When I try to call the method like this:

WebRequest initialRequest = CreateInitialUploadRequest("valid-parent-folder", "invalid file ç", "image/gif");

//I get an 'bad request' http error code 400 in the following line:
initialRequest.GetResponse();

我应该带code莫名其妙的要求?

Should I encode the request somehow?

推荐答案

继peleyal的评论我解决我的问题。我仍在使用REST调用和不会开车的API,但我检查了驱动API源$ C ​​$ c和发现,他们正在使用的Htt prequestMessage类。改变从我的WebRequest code到Htt的prequestMessage解决了这个问题:

Following the peleyal's commentary I solved my problem. I'm still using rest calls and not drive api, but I checked out the drive api source code and find out that they are using the HttpRequestMessage class. Changing my code from WebRequest to HttpRequestMessage solved the problem:

var request = new HttpRequestMessage(HttpMethod.Post, uri);
string strData = new JavaScriptSerializer().Serialize(jsonObject);

var content = new StringContent(strData, Encoding.UTF8, "application/json");
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
request.Content = content;

这篇关于无法上传文件名有特殊字符谷歌驱动REST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆