如何在box-view API头文件中指定参数?

时间:2015-03-30 13:23:35

标签: box-view-api

我正在使用以下一段C#代码通过Box视图API上传,转换和下载.pptx文件。

        var boxViewID = "";
        string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
        byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

        String url_ = @"https://upload.view-api.box.com/1/documents";
        HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url_);
        wr.ContentType = "multipart/form-data; boundary=" + boundary;
        wr.Headers.Add("Authorization:Token " + "MY_CODE"/*Configuration.BoxViewAPIKey*/);
        wr.Method = "POST";
        wr.KeepAlive = true;
        wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
        wr.Timeout = 1000000;            
        wr.SendChunked = true;
        DateTime start = DateTime.Now;
        Exception exc = null;
        Stream rs = wr.GetRequestStream();
        try
        {
            rs.Write(boundarybytes, 0, boundarybytes.Length);

            string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; non_svg=\"true\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
            string header = string.Format(headerTemplate,"file", file, contentType);
            Console.WriteLine(header);
            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
            rs.Write(headerbytes, 0, headerbytes.Length);
            FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[40960];
            int bytesRead = 0;

            int totalSent = 0;
            int totalLength = (int)fileStream.Length;

            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                totalSent += bytesRead;
                var percent = new decimal(100.0 * totalSent / totalLength);
                if (progress != null)
                {
                    progress("Box processing", percent);
                }
                rs.Write(buffer, 0, bytesRead);
            }
            fileStream.Close();
        }
        catch(Exception ex)
        {
            exc = ex;
        }
        DateTime end = DateTime.Now;
        int seconds = (int)(end - start).TotalSeconds;
        if(seconds>=0)
        {
            if(exc!=null)
            {
                throw exc;
            }
        }
        byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
        rs.Write(trailer, 0, trailer.Length);
        rs.Close();

        WebResponse wresp = null;
        try
        {
            wresp = wr.GetResponse();
            Stream stream2 = wresp.GetResponseStream();
            StreamReader reader2 = new StreamReader(stream2);
            var res = reader2.ReadToEnd();
            var docRes = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(res);
            if (docRes["id"] != null)
                boxViewID = docRes["id"];
        }
        catch (Exception ex)
        {
            if (wresp != null)
            {
                wresp.Close();
                wresp = null;
            }
        }
        finally
        {
            wr = null;
        }
        return boxViewID;

指定“non_svg”参数应为演示文稿中的每张幻灯片创建.png图像(而不是.svg + .html对)。但是,API似乎忽略了请求的这一部分,我总是得到svg文件。 我知道自己做错了什么吗?谢谢!

1 个答案:

答案 0 :(得分:0)

non_svg选项会为每个页面生成PNG表示,但仍会生成SVG表示。如果浏览器不支持SVG(基本上只有IE 8),查看器将只加载PNG文件。尝试在浏览器中将page-1.svg更改为page-1.png(例如https://view-api.box.com/1/sessions/465c5d45caf04752a6113b0e5df593a5/assets/page-1.png vs https://view-api.box.com/1/sessions/465c5d45caf04752a6113b0e5df593a5/assets/page-1.svg)。如果您使用文档content端点。

,则所有资产都将存在于content.zip中