在 ASP.NET Boilerplate 中上传图像 [英] Uploading image in ASP.NET Boilerplate

查看:36
本文介绍了在 ASP.NET Boilerplate 中上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发布图片时,HttpContext.Current.Requestnull.

有什么简单的方法可以实现吗?我在客户端使用 dropzone.js.

Is there any simple way to achieve this? I am using dropzone.js on client side.

项目是带有 Web API (ASP.NET Core 2.0) 模板的 Angular.

Project is Angular with Web API (ASP.NET Core 2.0) template.

[HttpPost]      
public HttpResponseMessage UploadJsonFile()
{
    HttpResponseMessage response = new HttpResponseMessage();           
    var httpRequest = HttpContext.Current.Request;
    if (httpRequest.Files.Count > 0)
    {
        foreach (string file in httpRequest.Files)
        {
            var postedFile = httpRequest.Files[file];
            var filePath = HttpContext.Current.Server.MapPath("~/UploadFile/" + postedFile.FileName);
            postedFile.SaveAs(filePath);
        }
    }
    return response;
}

推荐答案

这是我完成的代码,它工作正常.

This is the code which i had done it is working fine.

public void PostFile(IFormFile file)
{          
    var uploads = Path.Combine("ROOT PATH FOR THE FILES", "uploads");

    if (file.Length > 0)
    {
        var filePath = Path.Combine(uploads, file.FileName);

        using (var fileStream = new FileStream(filePath, FileMode.Create))
        {
            file.CopyTo(fileStream);
        }
    }
}

这篇关于在 ASP.NET Boilerplate 中上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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