如何处理内存流中的文件? [英] How do I process a file from a memory stream?

查看:48
本文介绍了如何处理内存流中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Vue.js前端发布一个或多个文件.它是这样完成的:

I am posting one or more files from my Vue.js frontend. It's being done as such:

<form enctype="multipart/form-data" novalidate v-if="isInitial || isSaving">
    <div class="dropbox">
        <input type="file" multiple :name="uploadFieldName" :disabled="isSaving" @change="filesChange($event.target.name, $event.target.files); fileCount = $event.target.files.length" accept=".json" class="input-file">
        <p v-if="isInitial">
            Drag files here to begin <br> or click to browse
        </p>
        <p v-if="isSaving">
            Uploading {{ fileCount }} files...
        </p>
    </div>
</form>

然后我通过这种方法发布它:

I then post it via this method:

function upload(formData) {
    const url = `${BASE_URL}/api/upload`;
    return axios.post(url, formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
          }
    })
}

似乎可以胜任这项工作.

Which seems to do the job.

根据Microsoft自己的指南,我创建了以下端点:

Following Microsoft's own guide, I have created the following endpoint:

namespace WebApplication6.Controllers
{
    public class UploadController : Controller
    {
        [HttpPost]
        [Route("/api/upload")]
        public async Task<IActionResult> UploadFiles(List<IFormFile> files)
        {
            long size = files.Sum(f => f.Length);

            // full path to file in temp location
            var filePath = Path.GetTempFileName();

            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await formFile.CopyToAsync(stream);
                    }
                }
            }

            // process uploaded files <-- What does this entail? How do I access the stream?

            return Ok(new { count = files.Count, size, filePath });
        }
    }
}

我对来自Java的C#有点陌生:

I am somewhat new to C# coming from Java: What does the Microsoft docs found here mean when they say "process uploaded files"?

如何从内存流访问它们?为什么注释不在for循环之外-如果我必须对文件进行处理,应该在该注释上?

How do I access them from the memory stream? Why is the comment outside the for loop - if I had to do stuff with the files, it should be on it - or not?

推荐答案

哇,微软的例子太可怕了.

Wow that Microsoft example is horrible.

假设您有一个名为 formFile IFormFile (我相信您可以自己编写该循环),那么最简单的方法就是:

Assuming you have a single IFormFile named formFile (I trust you be able to write that loop yourself), the simplest way to get to the barebones is:

using (var memoryStream = new MemoryStream())
{
    await formFile.CopyToAsync(memoryStream);
    byte[] bytes = memoryStream.ToArray();

    // do what you want with the bytes
}

您只是将字节复制了两次,分别是在原始格式文件中,在流中,然后复制到字节数组中,因此,当您对.NET有更多的经验时,可能需要对其进行一些优化.也许直接使用内存流而不是字节数组来进行处理.

You just copied the bytes twice, in the original formfile, in the stream and then into the byte array, so when you have more experience in .NET you may want to optimize it a bit. Maybe proceed your processing with the memory stream directly instead of the byte array.

这篇关于如何处理内存流中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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