C# 使用Restsharp PCL上传到dropbox
我正在尝试使用RestSharp.Portable使用PCL将文件上载到Dropbox。我的代码是C# 使用Restsharp PCL上传到dropbox,c#,dropbox-api,restsharp,portable-class-library,C#,Dropbox Api,Restsharp,Portable Class Library,我正在尝试使用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
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;
}
还有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(“文件”,文件名)代码>带有Dropbox的响应
{"error": "Forbidden"}
我做错了什么
注意:这个实现需要在一个PCL中,它将在WP8、Xamarin.Android、Xamarin.IOS和Windows WPF之间共享
更新:
虽然我以前尝试过PUT(files_PUT)api调用,但现在我已经成功了,因为我的应用程序只能访问自己的文件夹,所以我将路径改为sandbox而不是dropbox。以下是可能帮助他人的代码
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;
}
不是多部分表单上载。正如文档所说“…整个帖子正文将被视为文件”
我会就如何构造正确的HTTP请求提供更多建议,但老实说,我甚至从未使用过这个端点,我建议您也不要使用。正如文档中所说,“我们建议您改用它,因为它的界面更简单。”我建议您使用它,并将文件内容添加为请求的主体。感谢您发布了一个有效的解决方案。当网上没有很多关于上传二进制文件这类被认为是简单的事情时,这会有所帮助。
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; }
}
}