簡體   English   中英

使用REST API在Dropbox上上傳文件

[英]Upload file on Dropbox using REST api

下面的代碼將文件上傳到具有相同名稱,大小和文件類型的服務器上(就像它上傳虛擬文件一樣)。 但是當我嘗試查看它時,它顯示了注意。 當我嘗試上傳.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;
            }
        }

我從這些類中假定您正在使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM