将Excel文件传递到WEB API Web服务的最佳方法是什么 [英] What is the best way to pass an Excel file to a WEB API web service

查看:105
本文介绍了将Excel文件传递到WEB API Web服务的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将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.

如何接受文件POST

发送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
}

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆