• C#开发BIMFACE系列6 服务端API之获取文件信息


    《C#开发BIMFACE系列4 服务端API之源上传文件》《C#开发BIMFACE系列5 服务端API之文件直传》两篇文章中详细介绍了如何将本地文件上传到BIMFACE服务器及BIMFACE后台的分布式存储系统中。文件上传成功后,BIMFACE的服务会返回与该文件相关的信息,如下图:

     开发者在成功上传了文件并获得相关文件信息后,可以将信息保存到数据库中供后续的业务开发使用。

    除此之外,BIMFACE平台还提供了单独的服务用于获取文件信息、获取文件信息列表、获取文件上传的状态信息、获取应用支持的文件类型。

    下面分别介绍各种服务的使用方法。

    获取文件信息

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

    说明:根据文件ID获取文件详细信息

    参数:

     

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

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

    HTTP响应示例(200):

    {
      "code" : "success",
      "data" : {
        "createTime" : "2017-11-09 13:25:03", // 文件的上传时间
        "etag" : "19349858cjs98ericu989",     // 存储文件的额外属性
        "fileId" : 1216113551663296,          // 文件编号
        "length" : 39044,                     // 文件的大小
        "name" : "-1F.rvt",                   // 文件的名称
        "status" : "success",                 // 文件的上传状态
        "suffix" : "rvt"                      // 文件的后缀名
      },
      "message" : ""
    }

    C#实现方法:

     1 /// <summary>
     2 ///   根据文件ID获取文件详细信息
     3 /// </summary>
     4 /// <param name="accessToken">令牌</param>
     5 /// <param name="fileId">文件ID</param>
     6 /// <returns></returns>
     7 public virtual FileInfoGetResponse GetFileInfo(string accessToken, string fileId)
     8 {
     9     //GET https://file.bimface.com/files/{fileId}
    10     string url = string.Format(BimfaceConstants.FILE_HOST + "/files/{0}", fileId);
    11 
    12     BimFaceHttpHeaders headers = new BimFaceHttpHeaders();
    13     headers.AddOAuth2Header(accessToken);
    14 
    15     try
    16     {
    17         FileInfoGetResponse 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<FileInfoGetResponse>();
    24         }
    25         else
    26         {
    27             response = new FileInfoGetResponse
    28             {
    29                 Message = httpResult.RefText
    30             };
    31         }
    32 
    33         return response;
    34     }
    35     catch (Exception ex)
    36     {
    37         throw new Exception("[根据文件ID获取文件详细信息]发生异常!", ex);
    38     }
    39 }
    其中引用的 httpManager.Get() 方法如下:
    /// <summary>
    /// HTTP-GET方法,(不包含body数据)。
    /// 发送 HTTP 请求并返回来自 Internet 资源的响应(HTML代码)
    /// </summary>
    /// <param name="url">请求目标URL</param>
    /// <returns>HTTP-GET的响应结果</returns>
    public HttpResult Get(string url)
    {
        return RequestString(url, null, HttpMethod.GET, null);
    }
     1 /// <summary>
     2 ///  HTTP请求(包含文本的body数据)
     3 /// </summary>
     4 /// <param name="url">请求目标URL</param>
     5 /// <param name="data">主体数据(普通文本或者JSON文本)。如果参数中有中文,请使用合适的编码方式进行编码,例如:gb2312或者utf-8</param>
     6 /// <param name="method">请求的方法。请使用 HttpMethod 的枚举值</param>
     7 /// <param name="contentType"><see langword="Content-type" /> HTTP 标头的值。请使用 ContentType 类的常量来获取</param>
     8 /// <returns></returns>
     9 private HttpResult RequestString(string url, string data, string method, string contentType)
    10 {
    11     HttpResult httpResult = new HttpResult();
    12     HttpWebRequest httpWebRequest = null;
    13 
    14     try
    15     {
    16         httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
    17         httpWebRequest.Method = method;
    18         httpWebRequest.Headers = HeaderCollection;
    19         httpWebRequest.CookieContainer = CookieContainer;
    20         if (!string.IsNullOrWhiteSpace(contentType))
    21         {
    22             httpWebRequest.ContentType = contentType;// 此属性的值存储在WebHeaderCollection中。如果设置了WebHeaderCollection,则属性值将丢失。所以放置在Headers 属性之后设置
    23         }
    24         httpWebRequest.UserAgent = _userAgent;
    25         httpWebRequest.AllowAutoRedirect = _allowAutoRedirect;
    26         httpWebRequest.ServicePoint.Expect100Continue = false;
    27 
    28         if (data != null)
    29         {
    30             httpWebRequest.AllowWriteStreamBuffering = true;
    31             using (Stream requestStream = httpWebRequest.GetRequestStream())
    32             {
    33                 requestStream.Write(EncodingType.GetBytes(data), 0, data.Length);//将请求参数写入请求流中
    34                 requestStream.Flush();
    35             }
    36         }
    37 
    38         HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
    39         if (httpWebResponse != null)
    40         {
    41             GetResponse(ref httpResult, httpWebResponse);
    42             httpWebResponse.Close();
    43         }
    44     }
    45     catch (WebException webException)
    46     {
    47         GetWebExceptionResponse(ref httpResult, webException);
    48     }
    49     catch (Exception ex)
    50     {
    51         GetExceptionResponse(ref httpResult, ex, method, contentType);
    52     }
    53     finally
    54     {
    55         if (httpWebRequest != null)
    56         {
    57             httpWebRequest.Abort();
    58         }
    59     }
    60 
    61     return httpResult;
    62 }
    测试
     在BIMFACE的控制台中可以看到我们上传的文件列表

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


    可以看到获取文件信息成功,返回了以下信息:文件的上传时间、存储文件的额外属性、文件编号、文件的大小、文件的名称、文件的上传状态、文件的后缀名。
    测试程序如下:
     1 // 获取文件信息
     2 protected void btnGetFileInfo_Click(object sender, EventArgs e)
     3 {
     4     txtFileInfo.Text = string.Empty;
     5 
     6     string token = txtAccessToken.Text;
     7     string fileId = txtFileId.Text;
     8 
     9     FileApi api = new FileApi();
    10     FileInfoGetResponse response = api.GetFileInfo(token, fileId);
    11 
    12     txtFileInfo.Text = response.Code
    13                      + Environment.NewLine
    14                      + response.Message
    15                      + Environment.NewLine
    16                      + response.Data.ToString();
    17 }
    
    
    
  • 相关阅读:
    Java 读取某个目录下所有文件、文件夹
    poi根据excel模板导出Excel
    UML关系(泛化,实现,依赖,关联(聚合,组合))
    使用Git下载Hadoop的到本地Eclipse开发环境
    maven command to create your application
    Roo中的@Version
    oracle 给用户赋表空间
    hiberante 二级缓存设置
    procedure
    Oracle综合数据库管理命令集
  • 原文地址:https://www.cnblogs.com/SavionZhang/p/11433179.html
Copyright © 2020-2023  润新知