Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.net mvc中保存用户上载的图像_C#_Asp.net Mvc_Image_Aurelia - Fatal编程技术网

C# 在asp.net mvc中保存用户上载的图像

C# 在asp.net mvc中保存用户上载的图像,c#,asp.net-mvc,image,aurelia,C#,Asp.net Mvc,Image,Aurelia,我正在尝试使用aurelia将上传的图像从前端发布到asp.NETMVC。是否有办法将收到的图像以png格式保存在服务器上的文件夹中 我发布图像数据的Javascript方法(文件是上传的图像,id是我需要用于文件名的唯一图像id) ImagesController(Asp.net MVC) 使用Microsoft.AspNetCore.Mvc; 使用制度; 使用System.IO; 使用System.Linq; 使用System.Threading.Tasks; 命名空间SPAproject.

我正在尝试使用aurelia将上传的图像从前端发布到asp.NETMVC。是否有办法将收到的图像以png格式保存在服务器上的文件夹中

我发布图像数据的Javascript方法(文件是上传的图像,id是我需要用于文件名的唯一图像id)

ImagesController(Asp.net MVC)

使用Microsoft.AspNetCore.Mvc;
使用制度;
使用System.IO;
使用System.Linq;
使用System.Threading.Tasks;
命名空间SPAproject.Controllers
{
公共类ImagesController:控制器
{
[HttpPost,路由(“api/[controller]”)
公共异步任务SaveImage()
{
尝试
{
var form=wait Request.ReadFormAsync();
var file=form.Files.First();
var id=form.ElementAt(1);
var path=“/images/”+id+“.png”;
Stream=file.OpenReadStream();
返回Ok(true);
}
捕获(例外情况除外)
{
var originalMessage=ex.Message;
while(例如InnerException!=null)
ex=ex.InnerException;
返回BadRequest($“{originalMessage}{ex.Message}”);
}
}
}
}
}

我假设您使用的是ASP.NET核心,而不是ASP.NET(使用Microsoft.AspNetCore.Mvc;)

您可以使用asp.net核心中的新类,即文件。它简化了流操作

[HttpPost("UploadFiles")]
public async Task<IActionResult> Post(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
    // Don't rely on or trust the FileName property without validation.

    return Ok(new { count = files.Count, size, filePath});
}
[HttpPost(“上传文件”)]
公共异步任务发布(列表文件)
{
long size=files.Sum(f=>f.Length);
//临时位置中文件的完整路径
var filePath=Path.GetTempFileName();
foreach(文件中的var formFile)
{
如果(formFile.Length>0)
{
使用(var stream=newfilestream(filePath,FileMode.Create))
{
等待formFile.CopyToAsync(流);
}
}
}
//处理上载的文件
//未经验证,请勿依赖或信任FileName属性。
返回Ok(新的{count=files.count,size,filePath});
}

此外,您可以在此处找到完整的文档:

我正在为后端使用该代码的一个改编版本,我假设它正在工作,因为我的错误代码从错误代码400变为500。所以我的结论是我的aurelia fetch请求是错误的。你知道我怎么解决这个问题吗?将catch与fetch一起使用会记录错误,并且不会保存任何图像。
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace SPAproject.Controllers
{
    public class ImagesController : Controller
    {
        [HttpPost, Route("api/[controller]")]
        public async Task<IActionResult> SaveImage()
        {
            try
            {
                var form = await Request.ReadFormAsync();
                var file = form.Files.First();

                var id = form.ElementAt(1);
                var path = "/images/" + id + ".png";
                Stream stream = file.OpenReadStream();

                return Ok(true);
            }
            catch (Exception ex)
            {
                var originalMessage = ex.Message;

                while (ex.InnerException != null)
                    ex = ex.InnerException;
                return BadRequest($"{originalMessage} | {ex.Message}");
            }
        }
    }
}
}
string fileName = string.Format("{0}{1}", id, ".png");
var serverPath = Server.MapPath("~/images");
var path = Path.Combine(serverPath, fileName);

using (Stream inputStream = file.OpenReadStream())
using (var outputStream = new FileStream(path, FileMode.Create))
{
    // dump a stream to a file
    inputStream.CopyTo(outputStream);
}
[HttpPost("UploadFiles")]
public async Task<IActionResult> Post(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
    // Don't rely on or trust the FileName property without validation.

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