C#開發BIMFACE系列8 服務端API之獲取文件上傳狀態資訊
- 2019 年 10 月 5 日
- 筆記
系列目錄 【已更新最新開發文章,點擊查看詳細】
在BIMFACE控制台上傳文件,上傳過程及結束後它會自動告訴你文件的上傳狀態,目前有三種狀態:uploading,success,failure。即上傳中、上傳成功、上傳失敗。
如果是通過調用服務介面來上傳文件,上傳結束後也可以再調用BIMFACE提供的「獲取文件上傳狀態資訊」介面來查詢狀態。
下面詳細介紹如何獲取文件上傳狀態資訊。
請求地址:GET https://file.bimface.com/files/{fileId}/uploadStatus
說明:根據文件ID獲取文件上傳狀態資訊
參數:

請求 path(示例):https://file.bimface.com/files/1419273043501216/uploadStatus
請求 header(示例):"Authorization: Bearer dc671840-bacc-4dc5-a134-97c1918d664b"
HTTP響應示例(200):
{ "code" : "success", "data" : { "failedReason" : "input.stream.read.error", // 上傳失敗的遠因。如果上傳成功,則為空。 "fileId" : 1216113551663296, // 文件ID "name" : "-1F.rvt", // 文件名稱 "status" : "failure" // 文件上傳狀態 }, "message" : "" }
C#實現方法:
1 /// <summary> 2 /// 獲取文件上傳狀態資訊 3 /// </summary> 4 /// <param name="accessToken">令牌</param> 5 /// <param name="fileId">文件ID</param> 6 /// <returns></returns> 7 public virtual FileUploadStatusResponse GetFileUploadStatus(string accessToken, string fileId) 8 { 9 //GET https://file.bimface.com/files/{fileId}/uploadStatus 10 string url = string.Format(BimfaceConstants.FILE_HOST + "/files/{0}/uploadStatus", fileId); 11 12 BimFaceHttpHeaders headers = new BimFaceHttpHeaders(); 13 headers.AddOAuth2Header(accessToken); 14 15 try 16 { 17 FileUploadStatusResponse response; 18 19 HttpManager httpManager = new HttpManager(headers); 20 HttpResult httpResult = httpManager.Get(url); 21 if (httpResult.Status == HttpResult.STATUS_SUCCESS) 22 { 23 response = httpResult.Text.DeserializeJsonToObject<FileUploadStatusResponse>(); 24 } 25 else 26 { 27 response = new FileUploadStatusResponse 28 { 29 Message = httpResult.RefText 30 }; 31 } 32 33 return response; 34 } 35 catch (Exception ex) 36 { 37 throw new Exception("[獲取文件上傳狀態資訊]發生異常!", ex); 38 } 39 }
其中引用的 httpManager.Get() 方法,請參考《C#開發BIMFACE系列6 服務端API之獲取文件資訊》,方法完全一樣。
測試
在BIMFACE的控制台中可以看到我們上傳的文件列表

選擇任意一個文件的ID來做測試

可以看到獲取文件上傳狀態資訊成功,返回了以下資訊:失敗原因、文件編號、文件的名稱、文件的上傳狀態。
測試程式如下:
// 獲取文件上傳狀態資訊 protected void btnGetFileUploadStatus_Click(object sender, EventArgs e) { txtFileInfo.Text = string.Empty; string token = txtAccessToken.Text; string fileId = txtFileId.Text; FileApi api = new FileApi(); FileUploadStatusResponse response = api.GetFileUploadStatus(token, fileId); txtFileInfo.Text = response.Code + Environment.NewLine + response.Message + Environment.NewLine + response.Data.ToString(); }
系列目錄 【已更新最新開發文章,點擊查看詳細】