C#开发BIMFACE系列4 服务端API之源上传文件
BIMFACE二次开发系列目录
在注册成为BIMFACE的应用开发者后,要能在浏览器里浏览你的模型或者获取你模型内的BIM数据, 首先需要把你的模型文件上传到BIMFACE。根据不同场景,BIMFACE提供了丰富的文件相关的接口。
方式一:普通文件流上传 请求地址:PUT https://file.bimface.com/upload 说明:使用普通文件流上传,不支持表单方式;文件流需要在request body中传递。 参数:文件相关所有接口都需要提供有效的Access token。不支持View token。
内容类型(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 ///2 /// 普通文件流上传【不推荐使用该方式。推荐使用文件直传 UploadFileByPolicy()方法】 3 /// 4 /// 令牌 5 /// 【必填】文件的名称(不包含路径) 6 /// 文件流 7 /// 【可选】调用方的文件源ID,不能重复 8 /// 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 (); 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 ///2 /// 将数据缓冲区(一般是指文件流或内存流对应的字节数组)上载到由 URI 标识的资源。(包含body数据) 3 /// 4 /// 请求目标URL 5 /// 主体数据(字节数据)。如果没有请传递null 6 /// 请求的方法。请使用 WebRequestMethods.Http 的枚举值 7 /// HTTP 标头的值。请使用 ContentType 类的常量来获取。默认为 application/octet-stream 8 /// HTTP-POST的响应结果 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 }
1 ///方式二:指定外部文件url方式上传 如果需要上传的文件不在本地,且该文件可以通过指定的HTTP URL可以下载,BIMFACE支持直接传一个外部的HTTP文件URL, BIMFACE会去下载该文件,而无须用户先下载,再上传。 请求地址:PUT https://file.bimface.com/upload 说明:BIMFACE支持直接传一个外部的HTTP文件URL, BIMFACE会去下载该文件,而无须用户先下载,再上传。 参数:2 /// 将数据缓冲区(一般是指文件流或内存流对应的字节数组)上载到由 URI 标识的资源。(包含body数据) 3 /// 4 /// 请求目标URL 5 /// 主体数据(字节数据)。如果没有请传递null 6 /// 请求的方法。请使用 WebRequestMethods.Http 的枚举值 7 /// HTTP 标头的值。请使用 ContentType 类的常量来获取。默认为 application/octet-stream 8 /// HTTP-POST的响应结果 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 }
内容类型(ContentType):application/json
请求Path:https://file.bimface.com/upload?name=example.rvt&url=http(s)://xxxxxxxxxxx
请求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 ///2 /// 指定外部文件url方式上传文件 3 /// 4 /// 令牌 5 /// 【必填】文件的全名 6 /// 【必填】文件所在url 7 /// 【可选】调用方的文件源ID,不能重复 8 /// 【可选】文件etag 9 /// 10 public virtual FileUploadResponse UploadFileByUrl(string accessToken, string fileName, string fileUrl, string sourceId = "", string etag = "") 11 { 12 /* 如果需要上传的文件不在本地,且该文件可以通过指定的HTTP URL可以下载,BIMFACE支持直接传一个外部的HTTP文件URL, BIMFACE会去下载该文件,而无须用户先下载,再上传。 */ 13 14 //PUT 方式。例如:https://file.bimface.com/upload?name=example.rvt&url=http(s)://xxxxxxxxxxx 15 string url = string.Format(BimfaceConstants.FILE_HOST + "/upload?name={0}&url={1}", fileName.UrlEncode(Encoding.UTF8), fileUrl.UriEscapeDataString()); //文件的全名,使用URL编码(UTF-8),最多256个字符 16 if (sourceId.IsNotNullAndWhiteSpace()) 17 { 18 url = url + "&sourceId=" + sourceId; 19 } 20 if (etag.IsNotNullAndWhiteSpace()) 21 { 22 url = url + "&etag=" + etag; 23 } 24 25 BimFaceHttpHeaders headers = new BimFaceHttpHeaders(); 26 headers.AddOAuth2Header(accessToken); 27 28 try 29 { 30 FileUploadResponse response; 31 32 HttpManager httpManager = new HttpManager(headers); 33 HttpResult httpResult = httpManager.Put(url); 34 if (httpResult.Status == HttpResult.STATUS_SUCCESS) 35 { 36 response = httpResult.Text.DeserializeJsonToObject (); 37 } 38 else 39 { 40 response = new FileUploadResponse 41 { 42 Message = httpResult.RefText 43 }; 44 } 45 46 return response; 47 } 48 catch (Exception ex) 49 { 50 throw new Exception("指定外部文件url方式上传文件发生异常!", ex); 51 } 52 }
其中引用的 httpManager.Put() 方法如下:
1 ///2 /// HTTP-PUT方法,(不包含body数据)。 3 /// 发送 HTTP 请求并返回来自 Internet 资源的响应(HTML代码) 4 /// 5 /// 请求目标URL 6 /// HTTP-POST的响应结果 7 public HttpResult Put(string url) 8 { 9 return RequestString(url, null, WebRequestMethods.Http.Put, null); 10 }
1 ///方式三:文件直传 参考。 BIMFACE二次开发系列目录2 /// HTTP请求(包含文本的body数据) 3 /// 4 /// 请求目标URL 5 /// 主体数据(普通文本或者JSON文本)。如果参数中有中文,请使用合适的编码方式进行编码,例如:gb2312或者utf-8 6 /// 请求的方法。请使用 WebRequestMethods.Http 的枚举值 7 /// HTTP 标头的值。请使用 ContentType 类的常量来获取 8 /// 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 }