C# 在ASP.NET MVC中上载文件
当我选择一个文件并提交该文件进行上传时,我无法获取模型中文件路径的值。在控制器中,它显示为C# 在ASP.NET MVC中上载文件,c#,.net,asp.net-mvc,file-upload,C#,.net,Asp.net Mvc,File Upload,当我选择一个文件并提交该文件进行上传时,我无法获取模型中文件路径的值。在控制器中,它显示为null。我做错了什么 看法 模型 我没有VS来模拟你的问题。所以我不确定答案 试试这个,可能有用 <input type="file" name="model.FilePath" id="file" /> 如果不行的话,, 尝试在formcollection&HttpContext.Request.Files上查看它 它应该在那里。我认为模型绑定不适用于HttpPostedFileBas
null
。我做错了什么
看法
模型
我没有VS来模拟你的问题。所以我不确定答案 试试这个,可能有用
<input type="file" name="model.FilePath" id="file" />
如果不行的话,,
尝试在formcollection&HttpContext.Request.Files上查看它
它应该在那里。我认为模型绑定不适用于
HttpPostedFileBase
如果您将其从ViewModel中取出,并按如下方式操作,则它应该可以工作:
public ActionResult Profile(HttpPostedFileBase filePath)
{
string path = Convert.ToString(filePath);
return View();
}
HTHs,查尔斯
附:这篇文章可以帮助解释一些事情:我不使用HttpPostedFileBase,而是使用ifformfile
public class ModelFile
{
.....
public string Path{ get; set; }
public ICollection<IFormFile> Upload { get; set; }
}
public async Task<IActionResult> Index([Bind("Upload,Path")] ModelFile modelfile)
{
...
msg = await storeFilesInServer(modelfile.Upload,modelfile.Path);
}
private async Task<Message> storeFilesInServer(ICollection<IFormFile> upload, string path)
{
Message msg = new Message();
msg.Status = "OK";
msg.Code = 100;
msg.Text = "File uploaded successfully";
msg.Error = "";
string tempFileName = "";
try
{
foreach (var file in upload)
{
if (file.Length > 0)
{
string tempPath = path + @"\";
if (Request.Host.Value.Contains("localhost"))
tempPath = path + @"\";
using (var fileStream = new FileStream(tempPath + file.FileName, FileMode.Create))
{
await file.CopyToAsync(fileStream);
tempFileName = file.FileName;
}
}
msg.Text = tempFileName;
}
}
catch (Exception ex)
{
msg.Error = ex.Message;
msg.Status = "ERROR";
msg.Code = 301;
msg.Text = "There was an error storing the files, please contact support team";
}
return msg;
}
公共类模型文件
{
.....
公共字符串路径{get;set;}
公共ICollection上载{get;set;}
}
公共异步任务索引([Bind(“Upload,Path”)]ModelFile ModelFile)
{
...
msg=wait storeFilesInServer(modelfile.Upload,modelfile.Path);
}
专用异步任务StoreFileInserver(ICollection上载,字符串路径)
{
Message msg=新消息();
msg.Status=“OK”;
msg.Code=100;
msg.Text=“文件上传成功”;
msg.Error=“”;
字符串tempFileName=“”;
尝试
{
foreach(上传中的var文件)
{
如果(file.Length>0)
{
字符串tempPath=path+@“\”;
if(Request.Host.Value.Contains(“localhost”))
tempPath=路径+@“\”;
使用(var fileStream=newfilestream(tempPath+file.FileName,FileMode.Create))
{
等待file.CopyToAsync(fileStream);
tempFileName=file.FileName;
}
}
msg.Text=tempFileName;
}
}
捕获(例外情况除外)
{
msg.Error=ex.Message;
msg.Status=“ERROR”;
msg.Code=301;
msg.Text=“存储文件时出错,请与支持团队联系”;
}
返回味精;
}
是否真的可以为文件上载元素进行数据绑定?目前我会说不…那么也许你们不能像Olaf评论的那个样将文件上传元素绑定到一个模型参数上。我试图查看我以前的项目代码&我使用HttpContext.Request.Files,而不是将其绑定到模型。
<input type="file" name="model.FilePath" id="file" />
public ActionResult Profile(HttpPostedFileBase filePath)
{
string path = Convert.ToString(filePath);
return View();
}
public class ModelFile
{
.....
public string Path{ get; set; }
public ICollection<IFormFile> Upload { get; set; }
}
public async Task<IActionResult> Index([Bind("Upload,Path")] ModelFile modelfile)
{
...
msg = await storeFilesInServer(modelfile.Upload,modelfile.Path);
}
private async Task<Message> storeFilesInServer(ICollection<IFormFile> upload, string path)
{
Message msg = new Message();
msg.Status = "OK";
msg.Code = 100;
msg.Text = "File uploaded successfully";
msg.Error = "";
string tempFileName = "";
try
{
foreach (var file in upload)
{
if (file.Length > 0)
{
string tempPath = path + @"\";
if (Request.Host.Value.Contains("localhost"))
tempPath = path + @"\";
using (var fileStream = new FileStream(tempPath + file.FileName, FileMode.Create))
{
await file.CopyToAsync(fileStream);
tempFileName = file.FileName;
}
}
msg.Text = tempFileName;
}
}
catch (Exception ex)
{
msg.Error = ex.Message;
msg.Status = "ERROR";
msg.Code = 301;
msg.Text = "There was an error storing the files, please contact support team";
}
return msg;
}