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" : ,
"length" : ,
"name" : "-1F.rvt",
"status" : "success",
"suffix" : "rvt"
},
"message" : ""
}

C#实现方法

 /// <summary>
/// 普通文件流上传【不推荐使用该方式。推荐使用文件直传 UploadFileByPolicy()方法】
/// </summary>
/// <param name="accessToken">令牌</param>
/// <param name="fileName">【必填】文件的名称(不包含路径)</param>
/// <param name="fileStream">文件流</param>
/// <param name="sourceId">【可选】调用方的文件源ID,不能重复</param>
/// <returns></returns>
public virtual FileUploadResponse UploadFileByStream(string accessToken, string fileName, Stream fileStream, string sourceId = "")
{
/* 重要提示:使用普通文件流上传,不支持表单方式; 文件流需要在 request body 中传递 */ //PUT 方式。例如:https://file.bimface.com/upload?name=3F.rvt
string url = string.Format(BimfaceConstants.FILE_HOST + "/upload?name={0}", fileName.UrlEncode(Encoding.UTF8)); //文件的全名,使用URL编码(UTF-8),最多256个字符
if (sourceId.IsNotNullAndWhiteSpace())
{
url = url + "&sourceId=" + sourceId;
} byte[] fileBytes = fileStream.ToByteArray(); BimFaceHttpHeaders headers = new BimFaceHttpHeaders();
headers.AddOAuth2Header(accessToken); try
{
FileUploadResponse response; HttpManager httpManager = new HttpManager(headers);
HttpResult httpResult = httpManager.UploadData(url, fileBytes, WebRequestMethods.Http.Put);
if (httpResult.Status == HttpResult.STATUS_SUCCESS)
{
response = httpResult.Text.DeserializeJsonToObject<FileUploadResponse>();
}
else
{
response = new FileUploadResponse
{
Message = httpResult.RefText
};
} return response;
}
catch (Exception ex)
{
throw new Exception("普通文件流上时发生异常!", ex);
}
}

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

 /// <summary>
/// 将数据缓冲区(一般是指文件流或内存流对应的字节数组)上载到由 URI 标识的资源。(包含body数据)
/// </summary>
/// <param name="url">请求目标URL</param>
/// <param name="data">主体数据(字节数据)。如果没有请传递null</param>
/// <param name="method">请求的方法。请使用 WebRequestMethods.Http 的枚举值</param>
/// <param name="contentType"><see langword="Content-type" /> HTTP 标头的值。请使用 ContentType 类的常量来获取。默认为 application/octet-stream</param>
/// <returns>HTTP-POST的响应结果</returns>
public HttpResult UploadData(string url, byte[] data, string method = WebRequestMethods.Http.Post, string contentType = HttpContentType.APPLICATION_OCTET_STREAM)
{
return RequestData(url, data, method, contentType);
}
 /// <summary>
/// 将数据缓冲区(一般是指文件流或内存流对应的字节数组)上载到由 URI 标识的资源。(包含body数据)
/// </summary>
/// <param name="url">请求目标URL</param>
/// <param name="data">主体数据(字节数据)。如果没有请传递null</param>
/// <param name="method">请求的方法。请使用 WebRequestMethods.Http 的枚举值</param>
/// <param name="contentType"><see langword="Content-type" /> HTTP 标头的值。请使用 ContentType 类的常量来获取。默认为 application/octet-stream</param>
/// <returns>HTTP-POST的响应结果</returns>
private HttpResult RequestData(string url, byte[] data, string method = WebRequestMethods.Http.Post, string contentType = HttpContentType.APPLICATION_OCTET_STREAM)
{
HttpResult httpResult = new HttpResult();
HttpWebRequest httpWebRequest = null; try
{
httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
httpWebRequest.Method = method;
httpWebRequest.Headers = HeaderCollection;
httpWebRequest.CookieContainer = CookieContainer;
httpWebRequest.ContentType = contentType;
httpWebRequest.UserAgent = _userAgent;
httpWebRequest.AllowAutoRedirect = _allowAutoRedirect;
httpWebRequest.ServicePoint.Expect100Continue = false; if (data != null)
{
httpWebRequest.AllowWriteStreamBuffering = true;
httpWebRequest.ContentLength = data.Length; using (Stream requestStream = httpWebRequest.GetRequestStream())
{
requestStream.Write(data, , 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;
}
方式二:指定外部文件url方式上传
如果需要上传的文件不在本地,且该文件可以通过指定的HTTP URL可以下载,BIMFACE支持直接传一个外部的HTTP文件URL, BIMFACE会去下载该文件,而无须用户先下载,再上传。
请求地址:PUT https://file.bimface.com/upload
说明:BIMFACE支持直接传一个外部的HTTP文件URL, BIMFACE会去下载该文件,而无须用户先下载,再上传。
参数:
C#开发BIMFACE系列4 服务端API之源上传文件

内容类型(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" : ,
"length" : ,
"name" : "-1F.rvt",
"status" : "success",
"suffix" : "rvt"
},
"message" : ""
}

C#实现方法

 /// <summary>
/// 指定外部文件url方式上传文件
/// </summary>
/// <param name="accessToken">令牌</param>
/// <param name="fileName">【必填】文件的全名</param>
/// <param name="fileUrl">【必填】文件所在url</param>
/// <param name="sourceId">【可选】调用方的文件源ID,不能重复</param>
/// <param name="etag">【可选】文件etag</param>
/// <returns></returns>
public virtual FileUploadResponse UploadFileByUrl(string accessToken, string fileName, string fileUrl, string sourceId = "", string etag = "")
{
/* 如果需要上传的文件不在本地,且该文件可以通过指定的HTTP URL可以下载,BIMFACE支持直接传一个外部的HTTP文件URL, BIMFACE会去下载该文件,而无须用户先下载,再上传。 */ //PUT 方式。例如:https://file.bimface.com/upload?name=example.rvt&url=http(s)://xxxxxxxxxxx
string url = string.Format(BimfaceConstants.FILE_HOST + "/upload?name={0}&url={1}", fileName.UrlEncode(Encoding.UTF8), fileUrl.UriEscapeDataString()); //文件的全名,使用URL编码(UTF-8),最多256个字符
if (sourceId.IsNotNullAndWhiteSpace())
{
url = url + "&sourceId=" + sourceId;
}
if (etag.IsNotNullAndWhiteSpace())
{
url = url + "&etag=" + etag;
} BimFaceHttpHeaders headers = new BimFaceHttpHeaders();
headers.AddOAuth2Header(accessToken); try
{
FileUploadResponse response; HttpManager httpManager = new HttpManager(headers);
HttpResult httpResult = httpManager.Put(url);
if (httpResult.Status == HttpResult.STATUS_SUCCESS)
{
response = httpResult.Text.DeserializeJsonToObject<FileUploadResponse>();
}
else
{
response = new FileUploadResponse
{
Message = httpResult.RefText
};
} return response;
}
catch (Exception ex)
{
throw new Exception("指定外部文件url方式上传文件发生异常!", ex);
}
}

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

 /// <summary>
/// HTTP-PUT方法,(不包含body数据)。
/// 发送 HTTP 请求并返回来自 Internet 资源的响应(HTML代码)
/// </summary>
/// <param name="url">请求目标URL</param>
/// <returns>HTTP-POST的响应结果</returns>
public HttpResult Put(string url)
{
return RequestString(url, null, WebRequestMethods.Http.Put, null);
}
 /// <summary>
/// HTTP请求(包含文本的body数据)
/// </summary>
/// <param name="url">请求目标URL</param>
/// <param name="data">主体数据(普通文本或者JSON文本)。如果参数中有中文,请使用合适的编码方式进行编码,例如:gb2312或者utf-8</param>
/// <param name="method">请求的方法。请使用 WebRequestMethods.Http 的枚举值</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), , 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;
}
方式三:文件直传
 
上一篇:phoenix 开发API系列(三)phoenix api 结合数据库


下一篇:phoenix 开发API系列(二)phoenix 各类 api 实现方式