【问题标题】:Upload to dropbox using Restsharp PCL使用 Restsharp PCL 上传到 Dropbox
【发布时间】:2014-03-22 12:28:15
【问题描述】:

我正在尝试使用 RestSharp.Portable 使用 PCL 将文件上传到 Dropbox。我的代码是

public async Task<object> UploadFile(Stream fileStream, string fileName)
{
    var client = new RestClient("https://api-content.dropbox.com");
    client.ClearEncodings();
    client.AddEncoding("gzip", new GzipEncoding());

    var request = new RestRequest("1/files/dropbox/Apps/FileBolt", HttpMethod.Post);
    request.AddHeader("Authorization", string.Format("Bearer {0}", Token));
    request.AddParameter("file", fileName);

    byte[] bytes = null;
    long numBytes = fileStream.Length;

    using (var br = new BinaryReader(fileStream))
    {
        bytes = br.ReadBytes((int) numBytes);
    }

    request.AddFile(new FileParameter { ContentLength = numBytes, FileName = fileName, Name = "file", Value = bytes });

    var boxItemResponse = await client.Execute<Entities.Cloud.Dropbox.File>(request);
    if (boxItemResponse != null && boxItemResponse.Data != null)
    {
        return boxItemResponse.Data;
    }

    return null;
}

这是实际的 REST 调用

POST https://api-content.dropbox.com/1/files/dropbox/Apps/FileBolt HTTP/1.1
Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXX
Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
Accept-Encoding: gzip
Content-Type: multipart/form-data; boundary="0ab9510a-e347-4871-96c0-14b11b382435"
Host: api-content.dropbox.com
Content-Length: 20205
Expect: 100-continue

--0ab9510a-e347-4871-96c0-14b11b382435
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=file

driver.png
--0ab9510a-e347-4871-96c0-14b11b382435
Content-Length: 19865
Content-Disposition: form-data; name=file; filename=driver.png; filename*=utf-8''driver.png

{BYTES}
--0ab9510a-e347-4871-96c0-14b11b382435--

以及来自 DropBox 的响应

HTTP/1.1 400 Bad Request
Server: nginx
Date: Sat, 22 Mar 2014 12:16:07 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive

2e
{"error": {"file": "Expecting a file upload"}}
0

我还删除了 request.AddParameter("file", fileName); 并从 Dropbox 回复

{"error": "Forbidden"}

我在这里做错了什么?

注意:此实现需要位于 PCL 中,它将在 WP8、Xamarin.Android、Xamarin.IOS、Windows WPF 之间共享。

更新:

虽然我之前尝试过 PUT (files_put) api 调用,但我现在可以通过将路径更改为沙盒而不是保管箱,因为我的应用程序只能访问它自己的文件夹。这是可能对其他人有所帮助的代码。

public async Task<object> UploadFile(Stream fileStream, string fileName, string md5 = null)
{
    var client = new RestClient("https://api-content.dropbox.com");
    client.ClearEncodings();
    client.AddEncoding("gzip", new GzipEncoding());

    var request = new RestRequest(string.Format("1/files_put/sandbox/{0}", fileName), HttpMethod.Put);
    request.AddHeader("Authorization", string.Format("Bearer {0}", Token));

    byte[] bytes = null;
    long numBytes = fileStream.Length;

    using (var br = new BinaryReader(fileStream))
    {
        bytes = br.ReadBytes((int) numBytes);
    }

    var body = new Parameter
    {
        ContentType = new MediaTypeHeaderValue("application/octet-stream"),
        Name = "file",
        Value = bytes,
        Type = ParameterType.RequestBody,
        ValidateOnAdd = false
    };
    request.Parameters.Add(body);

    var response = await client.Execute<Entities.Cloud.Dropbox.File>(request);
    if (response != null && response.Data != null)
    {
        return response.Data;
    }

    return null;
}

这是响应实体

using System;
using Newtonsoft.Json;

namespace Entities.Cloud.Dropbox
{
    public class File
    {
        [JsonProperty(PropertyName = "size")]
        public string FriendlySize { get; set; }

        [JsonProperty(PropertyName = "bytes")]
        public int Size { get; set; }

        [JsonProperty(PropertyName = "path")]
        public string Path { get; set; }

        [JsonProperty(PropertyName = "is_dir")]
        public bool IsDirectory { get; set; }

        [JsonProperty(PropertyName = "is_deleted")]
        public bool IsDeleted { get; set; }

        [JsonProperty(PropertyName = "rev")]
        public string Revision { get; set; }

        [JsonProperty(PropertyName = "hash")]
        public string Hash { get; set; }

        [JsonProperty(PropertyName = "thumb_exists")]
        public bool ThumbnailExists { get; set; }

        [JsonProperty(PropertyName = "icon")]
        public string Icon { get; set; }

        [JsonProperty(PropertyName = "modified")]
        public DateTime Modified { get; set; }

        [JsonProperty(PropertyName = "root")]
        public string Root { get; set; }
    }
}

【问题讨论】:

  • 感谢您发布有效的解决方案。当网上没有太多关于上传二进制文件等所谓简单的事情时会有所帮助。

标签: c# dropbox-api restsharp portable-class-library


【解决方案1】:

/files (POST) 不是多部分表单上传。正如文档所说“......整个 POST 正文将被视为文件”。

我会就如何构建正确的 HTTP 请求提供更多建议,但老实说,我什至从未使用过这个端点,我建议你也不要这样做。正如文档所说,“我们建议您使用/files_put,因为它的界面更简单。”我建议使用它并将文件内容添加为请求的正文。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多