mvc razor中的FileUploadControl [英] FileUploadControl in mvc razor

查看:61
本文介绍了mvc razor中的FileUploadControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有

iam在我的mvc中使用文件上传控件,但它显示错误

这里是我的代码:

控制器:



公共ActionResult索引(Models.FileUpload文件)

{

试试

{



int maxLength = 1024 * 1021 * 5;

if(file.File.ContentLength> 0&& file .File.ContentLength< maxLength)

{

var fileName = Path.GetFileName(file.File.FileName);

string path = Path.Combine((Server.MapPath("〜/ E:/ sainath / MVC")),fileName);

file.File.SaveAs(path);

返回查看();

}





}

catch(例外ex)

{

ViewBag.Message ="错误上传Fi le" ;;

返回查看();



}

返回RedirectToAction(Index); < br $>




我的观点:

@using(Html.BeginForm(Index,Home,FormMethod .Post,new {enctype =multipart / form-data}))

{

< input type =fileid =fileUploadaria-selected =truerequired =required/>

< input type =submitvalue =Uploadname =Upload/>

}

Hi,all
iam using file upload control in my mvc but it showing an error
here is my code:
controller:

public ActionResult Index(Models.FileUpload file)
{
try
{

int maxLength = 1024 * 1021 * 5;
if (file.File.ContentLength > 0 && file.File.ContentLength < maxLength)
{
var fileName = Path.GetFileName(file.File.FileName);
string path = Path.Combine((Server.MapPath("~/E:/sainath/MVC")), fileName);
file.File.SaveAs(path);
return View();
}


}
catch (Exception ex)
{
ViewBag.Message = "Error Uploading File";
return View();

}
return RedirectToAction("Index");


My View:
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" id="fileUpload" aria-selected="true" required="required" />
<input type="submit" value="Upload" name="Upload" />
}

推荐答案

//To save uploaded files.
HttpFileCollectionBase hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
    HttpPostedFileBase hpf = hfc[i];
    if (hpf.ContentLength > 0)
    {       
        var fileName = Path.GetFileName(hpf.FileName);
        string path = Path.Combine((Server.MapPath("~/E:/sainath/MVC")), fileName);        
        hpf.SaveAs(path);     
    }
}


这篇关于mvc razor中的FileUploadControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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