Sha*_*had 2 c# asp.net-mvc fileinfo
我正在尝试为文件大小创建一些验证.我的代码:
[HttpPost]
public ActionResult Index(FotoModel model)
{
TryUpdateModel(model);
if (ModelState.IsValid)
{
if (model != null && model.File != null)
{
var fileName = Path.GetFileName(model.File.FileName);
var fileExtension = Path.GetExtension(fileName);
long fileSize = new FileInfo(fileName).Length;
fileName = "file" + fileExtension;
var path = Path.Combine(Server.MapPath("~/Content/Images"), fileName);
if (fileSize < 55000)
{
model.File.SaveAs(path);
model.link = fileName.ToString();
}
else
{
ViewData["Size"] = "Akceptowane pliki: jpg, jpeg, png o maksymalnym rozmiarze 50 KB";
}
return View(model);
}
}
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
在这一行:
long fileSize = new FileInfo(fileName).Length;
Run Code Online (Sandbox Code Playgroud)
我收到错误:"无法找到文件".你知道我怎么解决这个问题?
在Asp.Net MVC中,我们必须使用HttpPostedFileBase上载文件,如下所示: -
public ActionResult Index(FotoModel model, HttpPostedFileBase file)
{
if (file != null)
{
int byteCount = file.ContentLength; // <---Your file Size or Length
.............
.............
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6403 次 |
| 最近记录: |