如何在asp.net核心上传文件? [英] How to upload files in asp.net core?

查看:196
本文介绍了如何在asp.net核心上传文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用一些模型数据使用Asp.net MVC 6上传文件或图像?
例子,我有一个这样的形式;

 < form> 
< input type =file>
< input type =textplaceholder =图片名称...>
< input type =textplaceholder =图片描述...>
< input type =submitvalue =Submit>
< / form>

我阅读了很多关于如何上传的教程,但是我没有看到任何上传的数据

另外,是否有图像处理的重新调整大小和图像水印Codeigniter图像处理类相同的图书馆? ( https://codeigniter.com/user_guide/libraries/image_lib.html

解决方案

您可以将 IFormFile 类型的新属性添加到您的视图模型



  public class CreatePost 
{
public string ImageCaption {set; get;}
public string ImageDescription {set; get;}
public IFormFile MyImage {set; get;}
}





  public IActionResult Create()
{
return CreatePost());
}

现在在您的Create视图中,查看模型,有一个表单标签,其enctype属性设置为multipart / form-data

  @model CreatePost 
< form asp-action =Createenctype =multipart / form-data>

< input asp-for =我mageCaption/>
< input asp-for =ImageDescription/>
< input asp-for =MyImage/>

< input type =submit/>
< / form>

然后你的HttpPost动作来处理表单发布

  [HttpPost] 
public IActionResult Create(CreatePost模型)
{
var img = model.MyImage;
var imgCaption = model.ImageCaption;
//用上面的数据做一些事
//要做:return something
}

如果要将文件上传到应用程序中的某个目录,应使用 IHostingEnvironment 来获取webroot路径。这是一个工作示例。

  public class HomeController:Controller 
{
private readonly IHostingEnvironment hostingEnvironment;
public HomeController(IHostingEnvironment环境)
{
hostingEnvironment = environment;


public IActionResult Create(CreatePost模型)
{
//根据需要对模型进行其他验证
if(model。 MyImage!= null)
{
var uploads = Path.Combine(hostingEnvironment.WebRootPath,uploads);
var filePath = Path.Combine(uploads,GetUniqueName(model.MyImage.FileName));
model.MyImage.CopyTo(new FileStream(filePath,FileMode.Create));
}
//执行:返回
返回RedirectToAction(Index,Home);

private string GetUniqueName(string fileName)
{
fileName = Path.GetFileName(fileName);
return Path.GetFileNameWithoutExtension(fileName)
+_
+ Guid.NewGuid()。ToString()。Substring(0,4)
+ Path.GetExtension(fileName );




$ b $ p $这将把文件保存到> wwwwroot 目录中上传文件夹,使用Guids生成的随机文件名(以防止覆盖同名文件)


How to upload files or images using Asp.net MVC 6 with some model data? Example, I have a form like this;

<form>
    <input type="file">
    <input type="text" placeholder="Image name...">
    <input type="text" placeholder="Image description...">
    <input type="submit" value="Submit">
</form>

I read many Tutorials in how to upload but I don't see anything uploading with some data like the form above.

Also, is there any library for image manipulation for re-sizing and Image Watermarking same as Codeigniter image manipulation class? (https://codeigniter.com/user_guide/libraries/image_lib.html

解决方案

You can add a new property of type IFormFile to your view model

public class CreatePost
{
   public string ImageCaption { set;get; }
   public string ImageDescription { set;get; }
   public IFormFile MyImage { set; get; }
}

and in your GET action

public IActionResult Create()
{
   return View(new CreatePost());
}

Now in your Create view which is strongly typed to our view model, have a form tag which has the enctype property set to "multipart/form-data"

@model CreatePost
<form asp-action="Create" enctype="multipart/form-data">   

    <input asp-for="ImageCaption"/>
    <input asp-for="ImageDescription"/>
    <input asp-for="MyImage"/>

    <input type="submit"/>
</form>

And your HttpPost action to handle the form posting

[HttpPost]
public IActionResult Create(CreatePost model)
{
   var img = model.MyImage;
   var imgCaption = model.ImageCaption;
   // do something with the above data
   // to do : return something
}

If you want to upload the file to some directory in your app, you should use IHostingEnvironment to get the webroot path. Here is a working sample.

public class HomeController : Controller
{
    private readonly IHostingEnvironment hostingEnvironment;
    public HomeController(IHostingEnvironment environment)
    {
        hostingEnvironment = environment;
    }
    [HttpPost]
    public IActionResult Create(CreatePost model)
    {
        // do other validations on your model as needed
        if (model.MyImage != null)
        {
            var uploads = Path.Combine(hostingEnvironment.WebRootPath, "uploads");
            var filePath = Path.Combine(uploads,GetUniqueName(model.MyImage.FileName));
            model.MyImage.CopyTo(new FileStream(filePath, FileMode.Create));    
        }
        // to do  : Return something
        return RedirectToAction("Index","Home");
    }
    private string GetUniqueName(string fileName)
    {
        fileName = Path.GetFileName(fileName);
        return  Path.GetFileNameWithoutExtension(fileName)
                  + "_" 
                  + Guid.NewGuid().ToString().Substring(0, 4) 
                  + Path.GetExtension(fileName);
    }
}

This will save the file to uploads folder inside wwwwroot directory of your app with a random file name generated using Guids ( to prevent overwriting of files with same name)

这篇关于如何在asp.net核心上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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