简体   繁体   English

如何读取将从 .Net Core API 中的表单上传的文件?

[英]How can I read a file which will be upload from a form in .Net Core API?

I create a method in my.Net Core API which will upload a file.我在 my.Net Core API 中创建了一个方法,它将上传一个文件。

[HttpPost]
public async Task<IActionResult> ReadFile(IFormFile file)
{
    return BadRequest(file);
}

I do a return BadRequest(file) in order to read what it send me on postman .我做了一个return BadRequest(file)以便阅读它在postman上发送给我的内容。

The result is this:结果是这样的:

{
    "contentDisposition": "form-data; name=\"file\"; filename=\"data.dat\"",
    "contentType": "application/octet-stream",
    "headers": {
        "Content-Disposition": [
            "form-data; name=\"file\"; filename=\"data.dat\""
        ],
        "Content-Type": [
            "application/octet-stream"
        ]
    },
    "length": 200,
    "name": "file",
    "fileName": "data.dat"

} }

I see on Microsoft documentation this:我在 Microsoft 文档中看到:

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
    // Read the stream to a string, and write the string to the console.
        String line = sr.ReadToEnd();
        Console.WriteLine(line);
}

But the user will have to choose a file to read in it, the application won't have to go in a folder and read a file.但是用户必须选择一个文件来读取它,应用程序不必进入文件夹并读取文件。

It is possible to do this?有可能这样做吗? And can I have some link to help me to do this?我可以有一些链接来帮助我做到这一点吗?

Update更新

I want to the method ReadFile read the content of my file which will be upload thinks to a form.我想要方法 ReadFile 读取我的文件的内容,这些内容将被上传到一个表单中。

So I will have a string which will have the content of my file and after that I will can all I wanted to do in this file.所以我将有一个字符串,其中包含我的文件的内容,之后我将可以在此文件中执行所有我想做的事情。

For example I have a file and in this file it is wrote LESSON , with the method ReadFile I will get the word lesson in a string.例如,我有一个文件,在这个文件中写着LESSON ,使用ReadFile方法,我将在字符串中得到单词 lesson 。

The file will be bound to your IFormFile param.该文件将绑定到您的IFormFile参数。 You can access the stream via:您可以通过以下方式访问流:

using (var stream = file.OpenReadStream())
{
    // do something with stream
}

If you want to read it as a string, you'll need an instance of StreamReader :如果您想将其作为字符串读取,则需要一个StreamReader实例:

string fileContents;
using (var stream = file.OpenReadStream())
using (var reader = new StreamReader(stream))
{
    fileContents = await reader.ReadToEndAsync();
}

In you Controller:在你的控制器中:

  1. Check if IFormFile file contains something检查IFormFile file是否包含某些内容
  2. Check if the file's extension is the one you are looking for (.dat)检查文件的扩展名是否是您要查找的文件扩展名 (.dat)
  3. Check if the file's Mime type is correct to avoid attacks检查文件的 Mime 类型是否正确以避免攻击

Then, if it is all right, call a Service class to read your file.然后,如果一切正常,调用一个服务类来读取你的文件。

In your Service, you can do something like that:在您的服务中,您可以这样做:

var result = new StringBuilder();
using (var reader = new StreamReader(file.OpenReadStream()))
{
    while (reader.Peek() >= 0)
        result.AppendLine(await reader.ReadLineAsync()); 
}
return result.ToString();

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在.Net Core API中实现上传表单? - How to implement an upload form in .Net Core API? 如果扩展名未知,如何从文件中读取 (GET) - .NET Core 6 API - How To Read (GET) from File if Extension is Unknown - .NET Core 6 API 如何使用 .net 核心 api 将表单数据从 angular 插入到 sqlserver? - how can i insert the form data from angular to sqlserver using .net core api? 从 WinForms 上传文件到 .Net Core 3.1 API - Upload file from WinForms to .Net Core 3.1 API 如何访问在ASP.NET Core中从jquery文件上传发送的其他表单数据? - How to access additional form data sent from jquery file upload in asp.net core? 如何制作带有文件上传的 Asp.Net Core 表单? - How to make an Asp.Net Core form with file upload? 如何在我上传文件的Asp.Net核心web api端点上进行集成测试? - How to do an integration test on my Asp.Net core web api endpoint where I upload a file? 如何从ASP.NET Web API发布请求中读取文件数据? - How can I read the file data from an ASP.NET web API post request? 如何在 .NET 核心 Web api 中将上传按钮添加到招摇 UI? - How can I add an upload button to swagger UI in .NET core web api? 如何将文件从 Angular 上传到 ASP.NET Core Web API - How to Upload File from Angular to ASP.NET Core Web API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM