2017-02-07 3 views
0

액세스 토큰을 사용하여 보관 용으로 압축 파일을 업로드하려고합니다. 코드는 압축이 풀린 파일 작동 아래 :C#을 사용하여 보관함에 압축 파일 업로드

private static async Task FileUploadToDropbox(string filePath, string fileName, byte[] fileContent) 
{ 
    var client = new DropboxClient("Access Token"); 

    const int chunkSize = 1024; 

    using (var stream = new MemoryStream(fileContent)) 
    { 
     int numChunks = (int)Math.Ceiling((double)stream.Length/chunkSize); 

     byte[] buffer = new byte[chunkSize]; 
     string sessionId = null; 

     for (var idx = 0; idx < numChunks; idx++) 
     { 
      var byteRead = stream.Read(buffer, 0, chunkSize); 

      using (MemoryStream memStream = new MemoryStream(buffer, 0, byteRead)) 
      { 
       if (idx == 0) 
       { 
        var result = await client.Files.UploadSessionStartAsync(body: memStream); 
        sessionId = result.SessionId; 
       } 

       else 
       { 
        UploadSessionCursor cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * idx)); 

        if (idx == numChunks - 1) 
        { 
         await client.Files.UploadSessionFinishAsync(cursor, new CommitInfo(filePath + "/" + fileName), memStream); 
        } 

        else 
        { 
         await client.Files.UploadSessionAppendV2Async(cursor, body: memStream); 
        } 
       } 
      } 
     } 
    } 
} 

하지만이 코드를 사용하여 압축 파일을 업로드하려고 할 때,이 드롭 박스에 빈 압축 파일을 업로드합니다. zip 파일을 바이트 배열로 읽고 위의 메서드에 전달합니다. 파일 크기가 동일하게 유지 되더라도 파일을 다운로드하여 압축을 풀면 압축 된 파일이 비어 있다고 표시됩니다.

답변

0
private static async Task FileUploadToDropbox(string filePath, string fileName, string fileSource) 
     { 
      using (var dbx = new DropboxClient("access Token")) 
      using (var fs = new FileStream(fileSource, FileMode.Open, FileAccess.Read)) 
      { 
       var updated = await dbx.Files.UploadAsync(
        (filePath + "/" + fileName), WriteMode.Overwrite.Instance, body: fs); 
      } 
     } 

나를 위해 일했다.

0

이 시도하십시오 : 방법 위

/// <summary> 
    /// Function to import local file to dropbox. 
    /// </summary> 
    public static async Task<bool> WriteFileToDropBox() 
    { 
     try 
     { 
      //Connecting with dropbox. 
      var file = "File path at dropbox"; 
      using (var dbx = new DropboxClient("Access Token")) 
      using (var fs = new FileStream("Path of file to be uploaded.") 
      { 
       var updated = await dbx.Files.UploadAsync(file, WriteMode.Add.Instance, body: fs); 
      } 
      return true; 
     } 
     catch (Exception err) 
     { 
      MessageBox.Show(err.Message); 
      return false; 
     } 
    } 
+0

@Akshay, 도움이된다면 다른 사람에게 도움이 될 수 있도록 답을 표시하십시오. –

+0

"스트림에서 읽기를 지원하지 않습니다."라는 예외가 발생합니다. 귀하의 코드를 사용하십시오. – Akshay