【已更新最新开发文章,点击查看详细】
C#开发BIMFACE系列4 服务端API之源上传文件

  在注册成为BIMFACE的应用开发者后,要能在浏览器里浏览你的模型或者获取你模型内的BIM数据, 首先需要把你的模型文件上传到BIMFACE。根据不同场景,BIMFACE提供了丰富的文件相关的接口。

文件相关所有接口都需要提供有效的Access token。不支持View token。

方式一:普通文件流上传
请求地址:PUT https://file.bimface.com/upload
说明:使用普通文件流上传,不支持表单方式;文件流需要在request body中传递。
参数:
C#开发BIMFACE系列4 服务端API之源上传文件

内容类型(ContentType):application/octet-stream

请求Path:https://file.bimface.com/upload?name=3F.rvt

请求Header:"Authorization: Bearer dc671840-bacc-4dc5-a134-97c1918d664b"。JSON格式。

请求体需要上传的文件流。

HTTP 响应示例

{
  "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 ///  普通文件流上传【不推荐使用该方式。推荐使用文件直传 UploadFileByPolicy()方法】
 3 /// </summary>
 4 /// <param name="accessToken">令牌</param>
 5 /// <param name="fileName">【必填】文件的名称(不包含路径)</param>
 6 /// <param name="fileStream">文件流</param>
 7 /// <param name="sourceId">【可选】调用方的文件源ID,不能重复</param>
 8 /// <returns></returns>
 9 public virtual FileUploadResponse UploadFileByStream(string accessToken, string fileName, Stream fileStream, string sourceId = "")
10 {
11     /* 重要提示:使用普通文件流上传,不支持表单方式; 文件流需要在 request body 中传递 */
12 
13     //PUT 方式。例如:https://file.bimface.com/upload?name=3F.rvt
14     string url = string.Format(BimfaceConstants.FILE_HOST + "/upload?name={0}", fileName.UrlEncode(Encoding.UTF8));  //文件的全名,使用URL编码(UTF-8),最多256个字符
15     if (sourceId.IsNotNullAndWhiteSpace())
16     {
17         url = url + "&sourceId=" + sourceId;
18     }
19 
20     byte[] fileBytes = fileStream.ToByteArray();
21 
22     BimFaceHttpHeaders headers = new BimFaceHttpHeaders();
23     headers.AddOAuth2Header(accessToken);
24 
25     try
26     {
27         FileUploadResponse response;
28 
29         HttpManager httpManager = new HttpManager(headers);
30         HttpResult httpResult = httpManager.UploadData(url, fileBytes, WebRequestMethods.Http.Put);
31         if (httpResult.Status == HttpResult.STATUS_SUCCESS)
32         {
33             response = httpResult.Text.DeserializeJsonToObject<FileUploadResponse>();
34         }
35         else
36         {
37             response = new FileUploadResponse
38             {
39                 Message = httpResult.RefText
40             };
41         }
42 
43         return response;
44     }
45     catch (Exception ex)
46     {
47         throw new Exception("普通文件流上时发生异常!", ex);
48     }
49 }

 其中引用的 httpManager.UploadData() 方法如下:

 1 /// <summary>
 2 ///  将数据缓冲区(一般是指文件流或内存流对应的字节数组)上载到由 URI 标识的资源。(包含body数据)
 3 /// </summary>
 4 /// <param name="url">请求目标URL</param>
 5 /// <param name="data">主体数据(字节数据)。如果没有请传递null</param>
 6 /// <param name="method">请求的方法。请使用 WebRequestMethods.Http 的枚举值</param>
 7 /// <param name="contentType"><see langword="Content-type" /> HTTP 标头的值。请使用 ContentType 类的常量来获取。默认为 application/octet-stream</param>
 8 /// <returns>HTTP-POST的响应结果</returns>
 9 public HttpResult UploadData(string url, byte[] data, string method = WebRequestMethods.Http.Post, string contentType = HttpContentType.APPLICATION_OCTET_STREAM)
10 {
11     return RequestData(url, data, method, contentType);
12 }
C#开发BIMFACE系列4 服务端API之源上传文件
 1 /// <summary>
 2 ///  将数据缓冲区(一般是指文件流或内存流对应的字节数组)上载到由 URI 标识的资源。(包含body数据)
 3 /// </summary>
 4 /// <param name="url">请求目标URL</param>
 5 /// <param name="data">主体数据(字节数据)。如果没有请传递null</param>
 6 /// <param name="method">请求的方法。请使用 WebRequestMethods.Http 的枚举值</param>
 7 /// <param name="contentType"><see langword="Content-type" /> HTTP 标头的值。请使用 ContentType 类的常量来获取。默认为 application/octet-stream</param>
 8 /// <returns>HTTP-POST的响应结果</returns>
 9 private HttpResult RequestData(string url, byte[] data, string method = WebRequestMethods.Http.Post, string contentType = HttpContentType.APPLICATION_OCTET_STREAM)
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         httpWebRequest.ContentType = contentType;
21         httpWebRequest.UserAgent = _userAgent;
22         httpWebRequest.AllowAutoRedirect = _allowAutoRedirect;
23         httpWebRequest.ServicePoint.Expect100Continue = false;
24 
25         if (data != null)
26         {
27             httpWebRequest.AllowWriteStreamBuffering = true;
28             httpWebRequest.ContentLength = data.Length;
29 
30             using (Stream requestStream = httpWebRequest.GetRequestStream())
31             {
32                 requestStream.Write(data, 0, data.Length);
33                 requestStream.Flush();
34             }
35         }
36 
37         HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
38         if (httpWebResponse != null)
39         {
40             GetResponse(ref httpResult, httpWebResponse);
41             httpWebResponse.Close();
42         }
43     }
44     catch (WebException webException)
45     {
46         GetWebExceptionResponse(ref httpResult, webException);
47     }
48     catch (Exception ex)
49     {
50         GetExceptionResponse(ref httpResult, ex, method, contentType);
51     }
52     finally
53     {
54         if (httpWebRequest != null)
55         {
56             httpWebRequest.Abort();
57         }
58     }
59 
60     return httpResult;
61 }
View Code

相关文章: