【问题标题】:Upload file on Dropbox using REST api使用 REST api 在 Dropbox 上上传文件
【发布时间】:2016-05-20 07:39:00
【问题描述】:

以下代码在服务器上上传具有相同名称、大小和文件类型的文件(就像上传虚拟文件一样)。但是我什么时候尝试查看它,它显示了注意。当我尝试上传.txt 文件时,它可以工作。怎么了?

public static void UploadFile(string accessToken,string path,HttpPostedFileBase file)
        {
            try
            {

                var client = new RestClient("https://content.dropboxapi.com/1/files_put/auto/Abc/" + file.FileName);
                var request = new RestRequest(Method.PUT);
                request.AddHeader("Authorization", "Bearer " + accessToken);
                request.AddHeader("Content-Type", file.ContentType);
                //request.AddHeader("Content-Length", file.ContentLength.ToString());                               
                request.AddFile("file", path);

                IRestResponse response = client.Execute(request);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

【问题讨论】:

    标签: c# asp.net asp.net-mvc-5 dropbox-api


    【解决方案1】:

    我从课程中假设您使用的是RestSharp?我不是很熟悉,但通过快速搜索,它看起来不像 AddFile 做你想做的事。 (这会设置多部分表单上传,这不是 Dropbox API 所期望的。)

    我认为你想要的是这样的东西(完全未经测试),而不是 request.AddFile(...)

    // Get a byte array of the file content. Note that this involves reading
    // the entire file into memory! I couldn't immediately find a way to work
    // with the stream itself in RestSharp.
    MemoryStream target = new MemoryStream();
    file.InputStream.CopyTo(target);
    byte[] data = target.ToArray();
    
    // Send those bytes as the body of your HTTP request.
    request.AddParameter("application/octet-stream", data, ParameterType.RequestBody);
    

    【讨论】:

    • 感谢您的关注。我已经尝试过了,但没有成功。
    • 它在那里创建一个文件,但是当我想预览它时它什么也不显示,当我检查文件大小时,文件大小以字节为单位
    • 文件大小是多少?它与您尝试存储的文件大小相同吗?更大?更小?文件的实际内容是什么样的(例如,如果您从命令行type 文件)?
    • 就像,如果我上传一个 8kb 的文件,但是当我在 Dropbox 上检查文件大小时,它会显示 13 个字节
    • 我建议的代码会发生这种情况吗? 13 个字节是多少?
    猜你喜欢
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 2016-03-13
    相关资源
    最近更新 更多