Google Apis V2 html mime/type

本文关键字:type mime html Apis V2 Google | 更新日期: 2023-09-27 18:25:51

我上传了doc,xls文件,但我不知道如何上传html文件。此代码上载文件,但不预览文件。上面写着"我们很抱歉预览不可用"。我必须设置什么样的哑剧类型?

 if (extension == ".htm" || extension == ".html")
                    {
                        File body = new File();
                        body.Title = Path.GetFileNameWithoutExtension(item); 
                        //body.Description = "A test document";
                        body.MimeType = "text/HTML";
                        byte[] byteArray = System.IO.File.ReadAllBytes(item);
                        System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
                        FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream,
                                                                                       "application/vnd.google-apps.file");
                        request.Convert = true;

Google Apis V2 html mime/type

我在使用Gmail API上传文件时遇到了一些问题。我不知道Files API是否工作相同,但是,您可以在调用Insert方法之前尝试对byteArray进行编码。实现这些方法:

protected static string Base64ForUrlEncode(string str)
        {
            StringBuilder result = new StringBuilder(Convert.ToBase64String(Encoding.ASCII.GetBytes(str)).TrimEnd('='));
            result.Replace('+', '-');
            result.Replace('/', '_');
            return result.ToString();
        }
protected static byte[] GetBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;
}

然后,在Insert方法之前调用它:

byte[] byteArray = System.IO.File.ReadAllBytes(item);
string base64 = Base64ForUrlEncode(System.Text.Encoding.UTF8.GetString(byteArray));
System.IO.MemoryStream stream = new System.IO.MemoryStream(GetBytes(base64));
FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "application/vnd.google-apps.file");

您也可以尝试将mime类型设置为小写字母的"text/html"。