无法更改文件的 MIME 类型

Unable to change file's mime type

我正在尝试将文件的 MIME 类型更改为 in the documentation 所描述的 "application/vnd.google-apps.folder"。但是,当我发出 Web 请求时,不会抛出任何错误,但 MIME 类型不会改变。

这是我的代码:

public string MimeType
{
    get
    {
        dynamic data = JsonConvert.DeserializeObject(JsonMetadata); //the json of the fileID
        return data.mimeType;
    }
    set
    {
        RandomMethods.CreateRequest( //custom method for creating HttpWebRequest
            "PATCH", //method
            string.Format("https://www.googleapis.com/drive/v3/files/{0}?access_token={1}", FileID, Auth.AccessToken), //url
            "application/json", //content type
            "{ \"mimeType\": \"" + value + "\"}" //body
        ).GetResponse().Dispose();
    }
}

get 正常工作,而 set 不正常。这让我感到困惑,因为我的 Name 变量工作正常,并且使用完全相同的方法,只是名称被换掉了。

这是我使用的CreateRequest方法:

public static HttpWebRequest CreateRequest(string method, string url, string contentType, string body)
{
    var req = WebRequest.Create(url) as HttpWebRequest;
    req.Method = method;
    req.ContentType = contentType;
    byte[] data = Encoding.UTF8.GetBytes(body);
    req.ContentLength = data.Length;
    using (Stream stream = req.GetRequestStream())
        stream.Write(data, 0, data.Length);
    return req;
}

我不明白您为什么要尝试将文件的 mime 类型更改为文件夹的 mime 类型 (application/vnd.google-apps.folder),这是不可行的,如果您想更改 Google Drive 文件 MIME 类型,例如 application/vnd.google-apps.spreadsheetapplication/vnd.google-apps.document, 你可以使用 Files: export 端点.

将文档 ID 和您要更改的 mimeType 作为参数传递,您将获得相同文档的响应,但采用所需的 mimeType。

要进一步了解所有可用的 MIME 类型,请查看 G Suite documents and corresponding export MIME types