将Excel文件传递到WEB API Web服务的最佳方法是什么 [英] What is the best way to pass an Excel file to a WEB API web service
问题描述
我需要将Excel文件传递到C#中实现的Web服务中.到目前为止,我创建的Web服务如下.
I need to pass an Excel file in to and web service implemented in C#. The web service I have created so far is as follows.
public void loadData([FromBody]string dataFile)
{
}
我还认为可以通过Base64字符串来传递Excel文件.这是一个好方法还是有更好的方法呢?
I also thought that it is accepatble to pass the Excel file is using a Base64 string; Is this a good way or are there any better ways to do this?
推荐答案
基本上,您将文件作为multipart/form-data HTTP请求的一部分进行传递.
Basically, you pass the file as part of a multipart/form-data HTTP request.
发送HTML表单数据ASP.NET Web API中的操作:文件上载和多部分MIME
要接收任何类型的二进制文件,请使用以下代码.但是,这是相当不安全的.确实,应该检查请求标头的内容类型和内容长度.
To receive a binary file of any type, use the following code. However, this is fairly unsafe. Really, the request headers should be checked for content type and content length.
public void loadData() {
/* get raw data */
string dataFile = Request.Content.ReadAsStringAsync().Result
}