代码之家  ›  专栏  ›  技术社区  ›  cbll

如何处理来自内存流的文件?

  •  0
  • cbll  · 技术社区  · 6 年前

    我正在从vue.js前端发布一个或多个文件。它是这样做的:

    <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>
    

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

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

    这似乎起到了作用。

    按照微软自己的指南,我创建了以下端点:

    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语言有点陌生: Microsoft docs found here 是指当他们说“处理上传的文件”?

    如何从内存流访问它们?为什么在for循环之外的注释-如果我不得不处理文件,它应该在上面-或者不在上面?

    2 回复  |  直到 6 年前
        1
  •  1
  •   nvoigt    6 年前

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

    假设你有单人间 IFormFile 命名 formFile (我相信你能自己写下这个循环),最简单的方法是:

    using (var memoryStream = new MemoryStream())
    {
        await formFile.CopyToAsync(memoryStream);
        byte[] bytes = memoryStream.ToArray();
    
        // do what you want with the bytes
    }
    

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

        2
  •  1
  •   ivcubr Freak    6 年前

    看起来微软文档可能有点误导人。

    foreach (var formFile in files)
    {
        if (formFile.Length > 0)
        {
            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await formFile.CopyToAsync(stream);
                // here you have access to each individual file
            }
        }
    }
    

    里面 using 语句,您可以操作 stream 对象,即将其保存到其唯一的文件路径,处理文件中的数据(如果 .csv )等。问题有点宽泛,但微软的文章可能会误导您。