【问题标题】:Google Apis V2 html mime/typeGoogle APIs V2 html mime/type
【发布时间】:2014-12-01 17:38:58
【问题描述】:

我上传 doc、xls 文件,但我不明白如何上传 html 文件。此代码上传文件但不预览文件。它说-“我们很抱歉预览不可用”。我必须设置什么 mime 类型?

 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;

【问题讨论】:

    标签: c# html upload google-api


    【解决方案1】:

    我在使用 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”,使用小写字母。

    【讨论】:

    • 电子邮件是什么?什么是包含?
    • 对不起,我从我的实现中复制了。在您的情况下不是emailbyteArray。我将编辑我的答案。
    • 你在“myme/type”中设置了什么?
    • 就像我在答案中所说:“text/html”,小写。
    • 它的获取网址,但我只能在 DocViewer 中查看它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 2011-07-29
    • 2016-12-27
    相关资源
    最近更新 更多