使用mvc4.0上传的Excel文件 [英] Excel File uploaded using mvc4.0

查看:105
本文介绍了使用mvc4.0上传的Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好伙计们有人告诉我如何使用mvc 4.0上传excel文件或如何在.cshtml文件中调用iframe标签内的aspx页面

谢谢

manoj

hello guys plz anyone tell me how to excel file uploaded using mvc 4.0 or how to call a aspx page inside iframe tag in .cshtml file
thank's
manoj

推荐答案

它可能对你有帮助。



从ASP.NET MVC 4 .Net中的Excel文件到数据库表导入数据[ ^ ]
It might help you.

Import Data from Excel File to Database Table in ASP.NET MVC 4 .Net[^]


[HttpGet]

public ActionResult FileUpload()

{

return查看();

}



[HttpPost]

public ActionResult FileUpload(HttpPostedFileBase file)

{

if(ModelState.IsValid)

{

if(file == null)

{

ModelState.AddModelError(文件,请上传您的文件);



}

否则if(file.ContentLength> 0)

{

int MaxContentLength = 1024 * 1024 * 3; // 3 MB

string [] AllowedFileExtensions = new string [] {.jpg,。gif,。png,。pdf,。xls,。xlsx };

if(!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('。')))))

{

ModelState.AddModelError(文件,请文件类型:+ string.Join(,,AllowedFileExtensions));

}

else if(file.ContentLength> MaxContentLength)

{

ModelState.AddModelError(文件,您的文件太大,允许的最大大小为:+ MaxContentLength + MB);

}

其他

{

// TO:DO

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

var pa th = Path.Combine(Server.MapPath(〜/ Content / Upload),fileName);

file.SaveAs(path);

ModelState.Clear() ;

ViewBag.Message =文件上传成功:+你的文件名:+ fileName;

}

}

}

返回查看();

}
[HttpGet]
public ActionResult FileUpload()
{
return View();
}

[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
if (file == null)
{
ModelState.AddModelError("File", "Please Upload Your file");

}
else if(file.ContentLength>0)
{
int MaxContentLength = 1024 * 1024 * 3; //3 MB
string[] AllowedFileExtensions = new string[] { ".jpg", ".gif", ".png", ".pdf",".xls",".xlsx" };
if (!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
{
ModelState.AddModelError("File", "Please file of type: " + string.Join(", ", AllowedFileExtensions));
}
else if (file.ContentLength > MaxContentLength)
{
ModelState.AddModelError("File", "Your file is too large, maximum allowed size is: " + MaxContentLength + " MB");
}
else
{
//TO:DO
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Content/Upload"), fileName);
file.SaveAs(path);
ModelState.Clear();
ViewBag.Message = "File uploaded successfully:"+ "your file name:"+fileName;
}
}
}
return View();
}


这篇关于使用mvc4.0上传的Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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