首页 > 解决方案 > 将文件上传到 .netcore 中指定的路径

问题描述

我将上传的文件保存在根目录下。如何将此添加到我指定的路径(@“C:\ UploadsFolder”)?

public JsonResult Test(FileModel model)
{
    string SavePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img", model.FormFile.FileName);

    using (var stream = new FileStream(SavePath, FileMode.Create))
    {
        model.FormFile.CopyTo(stream);
    }

    return Json("");
}

在 FileModel.cs 中

public class FileModel
{
    public string Files { get; set; }

    public IFormFile FormFile { get; set; }
}

标签: c#asp.net-corefile-uploadasp.net-core-mvc

解决方案


我认为这应该可以工作。因此,文件将上传到 C 驱动器而不是 wwroot。

string SavePath = Path.Combine(Directory.GetCurrentDirectory(), (@"C:\", model.FormFile.FileName);

推荐阅读