在《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# 实现方法:
- /// <summary>
- /// 根据文件 ID 获取文件详细信息
- /// </summary>
- /// <param name="accessToken"> 令牌 </param>
- /// <param name="fileId"> 文件 ID</param>
- /// <returns></returns>
- public virtual FileInfoGetResponse GetFileInfo(string accessToken, string fileId)
- {
- //GET https://file.bimface.com/files/{fileId}
- string url = string.Format(BimfaceConstants.FILE_HOST + "/files/{0}", fileId);
- BimFaceHttpHeaders headers = new BimFaceHttpHeaders();
- headers.AddOAuth2Header(accessToken);
- try
- {
- FileInfoGetResponse response;
- HttpManager httpManager = new HttpManager(headers);
- HttpResult httpResult = httpManager.Get(url);
- if (httpResult.Status == HttpResult.STATUS_SUCCESS)
- {
- response = httpResult.Text.DeserializeJsonToObject<FileInfoGetResponse>();
- }
- else
- {
- response = new FileInfoGetResponse
- {
- Message = httpResult.RefText
- };
- }
- return response;
- }
- catch (Exception ex)
- {
- throw new Exception("[根据文件 ID 获取文件详细信息] 发生异常!", ex);
- }
- }
其中引用的 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);
- }
- /// <summary>
- /// HTTP 请求 (包含文本的 body 数据)
- /// </summary>
- /// <param name="url"> 请求目标 URL</param>
- /// <param name="data"> 主体数据 (普通文本或者 JSON 文本). 如果参数中有中文, 请使用合适的编码方式进行编码, 例如: gb2312 或者 utf-8</param>
- /// <param name="method"> 请求的方法. 请使用 HttpMethod 的枚举值 </param>
- /// <param name="contentType"><see langword="Content-type" /> HTTP 标头的值. 请使用 ContentType 类的常量来获取 </param>
- /// <returns></returns>
- private HttpResult RequestString(string url, string data, string method, string contentType)
- {
- HttpResult httpResult = new HttpResult();
- HttpwebRequest httpWebRequest = null;
- try
- {
- httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
- httpWebRequest.Method = method;
- httpWebRequest.Headers = HeaderCollection;
- httpWebRequest.CookieContainer = CookieContainer;
- if (!string.IsNullOrWhiteSpace(contentType))
- {
- httpWebRequest.ContentType = contentType;// 此属性的值存储在 WebHeaderCollection 中. 如果设置了 WebHeaderCollection, 则属性值将丢失. 所以放置在 Headers 属性之后设置
- }
- httpWebRequest.UserAgent = _userAgent;
- httpWebRequest.AllowAutoRedirect = _allowAutoRedirect;
- httpWebRequest.ServicePoint.Expect100Continue = false;
- if (data != null)
- {
- httpWebRequest.AllowWriteStreamBuffering = true;
- using (Stream requestStream = httpWebRequest.GetRequestStream())
- {
- requestStream.Write(EncodingType.GetBytes(data), 0, data.Length);// 将请求参数写入请求流中
- requestStream.Flush();
- }
- }
- HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
- if (httpWebResponse != null)
- {
- GetResponse(ref httpResult, httpWebResponse);
- httpWebResponse.Close();
- }
- }
- catch (WebException webException)
- {
- GetWebExceptionResponse(ref httpResult, webException);
- }
- catch (Exception ex)
- {
- GetExceptionResponse(ref httpResult, ex, method, contentType);
- }
- finally
- {
- if (httpWebRequest != null)
- {
- httpWebRequest.Abort();
- }
- }
- return httpResult;
- }
测试
在 BIMFACE 的控制台中可以看到我们上传的文件列表
选择任意一个文件的 ID 来做测试
可以看到获取文件信息成功, 返回了以下信息: 文件的上传时间, 存储文件的额外属性, 文件编号, 文件的大小, 文件的名称, 文件的上传状态, 文件的后缀名.
测试程序如下:
- // 获取文件信息
- protected void btnGetFileInfo_Click(object sender, EventArgs e)
- {
- txtFileInfo.Text = string.Empty;
- string token = txtAccessToken.Text;
- string fileId = txtFileId.Text;
- FileApi API = new FileApi();
- FileInfoGetResponse response = API.GetFileInfo(token, fileId);
- txtFileInfo.Text = response.Code
- + Environment.NewLine
- + response.Message
- + Environment.NewLine
- + response.Data.ToString();
- }
来源: https://www.cnblogs.com/SavionZhang/p/11433179.html