【问题标题】:Unable to change file's mime type无法更改文件的 mime 类型
【发布时间】:2023-03-06 12:54:02
【问题描述】:

我正在尝试将文件的 MIME 类型更改为 in the documentation 中所述的 "application/vnd.google-apps.folder"。但是,当我发出网络请求时,不会引发错误,但 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;
}

【问题讨论】:

    标签: c# google-api google-drive-api httpwebrequest mime-types


    【解决方案1】:

    我不明白您为什么要尝试将文件的 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

    【讨论】:

      猜你喜欢
      • 2015-03-10
      • 2010-11-30
      • 2016-05-23
      • 2021-07-19
      • 2012-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      相关资源
      最近更新 更多