C # development BIMFACE 10 series of server-side API to obtain the file download link

BIMFACE console or by calling the service interface to upload files successfully, you need to download the source files default scenario, downloading files generally you need to know the download link to the file. BIMACE platform provides a "get file download link," the service interface. The following details its use.

Request Address: GET https://file.bimface.com/download/url

Description: Application by downloading address of the interface to obtain the file, and then download the file. Download the effective time is 5 minutes.

parameter:

Request path (Example): https://file.bimface.com/download/url?fileId=1419273043501216

Request header (Example): "the Authorization: Bearer dc671840-BACC-4dc5-a134-97c1918d664b"

Example HTTP response (200):

{
  "code" : "success",
  "data" : "data",    // 请求成功后返回的文件下载链接
  "message" : ""
}

C#实现方法: 

 1 /// <summary>
 2 ///  获取文件下载链接
 3 /// </summary>
 4 /// <param name="accessToken">令牌</param>
 5 /// <param name="fileId">文件ID</param>
 6 /// <param name="name">文件名称</param>
 7 /// <returns></returns>
 8 public virtual FileDownloadUrlGetResponse GetFileDownloadUrl(string accessToken, string fileId, string name = "")
 9 {
10     //GET GET https://file.bimface.com/download/url
11     string url = string.Format(BimfaceConstants.FILE_HOST + "/download/url?fileId={0}", fileId);
12     if (name.IsNotNullAndWhiteSpace())
13     {
14         url = url + "&name=" + name.UriEscapeDataString();
15     }
16     BimFaceHttpHeaders headers = new BimFaceHttpHeaders();
17     headers.AddOAuth2Header(accessToken);
18 
19     try
20     {
21         FileDownloadUrlGetResponse response;
22 
23         HttpManager httpManager = new HttpManager(headers);
24         HttpResult httpResult = httpManager.Get(url);
25         if (httpResult.Status == HttpResult.STATUS_SUCCESS)
26         {
27             response = httpResult.Text.DeserializeJsonToObject<FileDownloadUrlGetResponse>();
28         }
29         else
30         {
31             response = new FileDownloadUrlGetResponse
32             {
33                 Message = httpResult.RefText
34             };
35         }
36 
37         return response;
38     }
39     catch (Exception ex)
40     {
41         throw new Exception("[获取文件下载链接]发生异常!", ex);
42     }
43 }
其中引用的 httpManager.Get() 方法,请参考《C#开发BIMFACE系列6 服务端API之获取文件信息》,方法完全一样。
测试
在BIMFACE的控制台中可以看到我们上传的文件列表

选择任意一个文件的ID来做测试

可以看到获取文件下载链接成功,返回了以下信息:文件下载链。

复制下载链接到浏览器的地址栏中,回车即开始下载文件

下载地址有效时间是5分钟,5分钟后该链接即失效。

如果再下载,需要重新获取下载链接。

测试程序如下:

// 获取文件下载链接
protected void btnGetFileDownloadUrl_Click(object sender, EventArgs e)
{
    txtFileInfo.Text = string.Empty;

    string token = txtAccessToken.Text;
    string fileId = txtFileId.Text;

    FileApi api = new FileApi();
    FileDownloadUrlGetResponse response = api.GetFileDownloadUrl(token, fileId);

    txtFileInfo.Text = response.Code
                     + Environment.NewLine
                     + response.Message
                     + Environment.NewLine
                     + response.Data.ToString();
}

 

Guess you like

Origin www.cnblogs.com/SavionZhang/p/11434093.html