从Dropbox获取文件并保存在我的服务器上时遇到问题

时间:2014-12-06 05:46:40

标签: c# dropbox-api

这是我从dropbox中选择文件的java脚本代码,当我尝试使用C#将此文件保存到服务器时,我能够看到服务器上的文件,但它是空的。当我尝试打开文件时,文件正在给出像文件这样的错误已损坏'使用signalR。

options = {

    // Required. Called when a user selects an item in the Chooser.
    success: function (files) {
        alert("Here's the file link: " + files[0].link)
        hub.server.servermethod(files[0].link, files[0].name);
    },

    // Optional. Called when the user closes the dialog without selecting a file
    // and does not include any parameters.
    cancel: function () {

    },

    // Optional. "preview" (default) is a preview link to the document for sharing,
    // "direct" is an expiring link to download the contents of the file. For more
    // information about link types, see Link types below.
    linkType: "preview", // or "direct"

    // Optional. A value of false (default) limits selection to a single file, while
    // true enables multiple file selection.
    multiselect: false, // or true

    // Optional. This is a list of file extensions. If specified, the user will
    // only be able to select files with these extensions. You may also specify
    // file types, such as "video" or "images" in the list. For more information,
    // see File types below. By default, all extensions are allowed.
    extensions: ['.csv', '.xls', '.tsv', '.xlsx', '.txt'],
};
var button = Dropbox.createChooseButton(options);
$('#container').append(button);
function some() {
    Dropbox.choose(options);
}

服务器方法代码是

       // execute the request
           HttpWebResponse response = (HttpWebResponse)
               request.GetResponse();
           // we will read data via the response stream
           Stream resStream = response.GetResponseStream();
           string tempString = null;
           int count = 0;
           Byte[] buffer = new Byte[32 * 1024];
           StringBuilder sb = new StringBuilder();
           do
           {
               // fill the buffer with data
               count = resStream.Read(buffer, 0, buffer.Length);

               // make sure we read some data
               if (count != 0)
               {
                   // translate from bytes to ASCII text
                   tempString = Encoding.ASCII.GetString(buffer, 0, count);

                   // continue building the string
                   sb.Append(tempString);
               }
           }
           while (count > 0); // any more data to read?
           using (FileStream fs = File.Create(System.Configuration.ConfigurationManager.AppSettings.GetValues("DocumentPath").First().ToString() + fileName))
           {
               // Byte[] bufer = new Byte[32 * 1024];
               fs.Write(buffer, 0, buffer.Length);
           }

1 个答案:

答案 0 :(得分:1)

您正在将linkType设置为"预览",它会为您提供指向文件预览页面的链接,而不是文件内容本身。如果您想直接访问文件内容,例如,要立即以编程方式将内容下载到您的服务器,就像您正在尝试的那样,您应该使用" direct" linkType