2017-03-07 62 views
1

我按照此guidance實施了一項Web服務,以將文件上傳到Azure存儲。如何從Android上傳PDF文件?

我已將它發佈到Azure。它從郵差工作正常。

我想使用Android的此服務上傳PDF文件。

我上次嘗試的代碼是這樣的:

new MultipartUploadRequest(this, uploadId, url) 
     .addFileToUpload(file.getAbsolutePath(), "file", file.getName(), ContentType.APPLICATION_PDF) 
     .addParameter("name", file.getName()) 
     .setDelegate(new MyUploadStatusDelegate()) 
     .setNotificationConfig(new UploadNotificationConfig()) 
     .setMaxRetries(0) 
     .startUpload(); 

這就產生了一個400響應,但我試過,結果好壞參半幾個變化。

我有一個調試器連接,所以我知道Web服務被調用。我目前無法進行調試,因此無法用此版本的調用說出服務器上發生的情況。

我希望得到這個Android代碼的任何幫助工作。我的頭受傷了。

回答

0

我不知道我發佈的代碼有什麼問題,但是在查看addFileToUpload的源代碼之後,我可以看到如果內容類型爲空,它會自行解決問題。我這樣做,嘿,它的工作。

0
try { 
      String uploadId = UUID.randomUUID().toString(); 

      //Creating a multi part request 
      new MultipartUploadRequest(this, uploadId, UPLOAD_URL) 
        .addFileToUpload(path, "f_url") //Adding file 
        .addParameter("category", "file") 
        .addHeader("Authorization", "Bearer " + token) //Adding token 
        .setNotificationConfig(new UploadNotificationConfig()) 
        .setMaxRetries(2) 
        .startUpload(); //Starting the upload 

     } catch (Exception exc) { 
      Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show(); 
     }