C#开发BIMFACE系列8 服务端API之获取文件上传状态信息

在BIMFACE控制台上传文件,上传过程及结束后它会自动告诉你文件的上传状态,目前有三种状态:uploading,success,failure。即上传中、上传成功、上传失败。html

若是是经过调用服务接口来上传文件,上传结束后也能够再调用BIMFACE提供的“获取文件上传状态信息”接口来查询状态。json

下面详细介绍如何获取文件上传状态信息。api

请求地址:GET https://file.bimface.com/files/{fileId}/uploadStatus测试

说明:根据文件ID获取文件上传状态信息url

参数:
spa

请求 path(示例):https://file.bimface.com/files/1419273043501216/uploadStatuscode

请求 header(示例):"Authorization: Bearer dc671840-bacc-4dc5-a134-97c1918d664b"orm

HTTP响应示例(200):htm

{
  "code" : "success",
  "data" : {
    "failedReason" : "input.stream.read.error", // 上传失败的远因。若是上传成功,则为空。
    "fileId" : 1216113551663296,                // 文件ID
    "name" : "-1F.rvt",                         // 文件名称
    "status" : "failure"                        // 文件上传状态
  },
  "message" : ""
}

C#实现方法:blog

 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();
}
相关文章
相关标签/搜索