【问题标题】:How to upload images to folder under wwwroot in .NET? (MVC)如何将图像上传到 .NET 中 wwwroot 下的文件夹? (MVC)
【发布时间】:2019-11-28 02:25:42
【问题描述】:

我正在尝试将图像文件保存到 wwwroot 下的文件夹,数据库应该存储文件的路径名,而 wwwroot 下的文件夹应该存储图像文件。

我想要实现的是将图像文件保存到wwwroot下的文件夹中。 和 要保存到本地数据库的图像文件的路径名。

谢谢!!

查看:

<form asp-controller="MobileSuits" asp-action="Add">
    <div class="form-horizontal">
        <h4>TheMobileSuit</h4>
        <hr />
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="Id" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="Id" class="form-control" />
                <span asp-validation-for="Id" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group">
            <label asp-for="Name" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="Name" class="form-control" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group">
            <label asp-for="PicFile" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                  @*<input type="file" name="photo"
                               class="form-control" />*@
                <input type="file" name="file" id="file" /> 
                <span asp-validation-for="PicFile" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
</form>

控制器:

[Authorize]
        public IActionResult Add()
        {
            return View();
        }

    [Authorize]
    [HttpPost]
    public IActionResult Add(TheMobileSuit themobilesuits, IFormFile photo)
    {

        DbSet<TheMobileSuit> dbs = _dbContext.TheMobileSuit;
        dbs.Add(themobilesuits);

        if (_dbContext.SaveChanges() == 1)
            TempData["Msg"] = "New Order Added!";
        else
            TempData["Msg"] = "Error.";
        return RedirectToAction("Index");
    }

【问题讨论】:

标签: .net asp.net-mvc image file-upload


【解决方案1】:

试试这个简单的。在这里你必须注入_hostingEnvironment,这样你才能得到ContentRootPath

  string folderName = "Upload/Profile/" + user.Id;
        string webRootPath = _hostingEnvironment.ContentRootPath;
        string newPath = Path.Combine(webRootPath, folderName);
        if (!Directory.Exists(newPath))
        {
            Directory.CreateDirectory(newPath);
        }
        string extention = file.ContentType.Split("/")[1];
        string fileName = user.Id + ".jpg";
        string fullPath = Path.Combine(newPath, fileName);
        string envpath = folderName + "/" + fileName;
        using (var stream = new FileStream(fullPath, FileMode.Create))
        {
            file.CopyTo(stream);
        }

【讨论】:

    【解决方案2】:
    First step would be create folder under wwwroot if it doesn't exists
    
    1. Directory.CreateDirectory(_hostingEnvironment.WebRootPath + "\\folderName\\");
    
    Second step would be append your file name  to  above path like below
    2.`var fullPathName = _hostingEnvironment.WebRootPath + "\\folderName\\" +             
    photo.file.FileName;`
    
    Final step would be by using FileStream you can copy the file
    3.`using (FileStream stream = System.IO.File.Create(fullPathName))
    {
    photo.file.CopyTo(stream);
    stream.Flush();
    }`
    

    【讨论】:

    • 嗨,您介意向我展示整个代码吗?我不明白你的意思...谢谢
    猜你喜欢
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多