如何在mvc中上传.pdf,.docx和.doc的文件文件类型 [英] How to upload a file file type of .pdf, .docx and .doc in mvc

查看:327
本文介绍了如何在mvc中上传.pdf,.docx和.doc的文件文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我可以轻松上传任何类型的文件,但我的要求是上传.pdf,.doc,.docx扩展名文件。



我有尝试但没有服用....请帮助我...

解决方案

以下链接为您提供了一个工作示例:http://stackoverflow.com/questions / 4328947 / limit-file-format-when-using-input-type-file


我相信这会有所帮助



  public   void 插入(FileloadView doc)
{
使用 var rep = new FileloadRepository())
{
rep.Insert(ConvertToDocuments(doc));
}
}

私有 静态 FileLoad ConvertToDocuments (FileloadView doc)
{
FileLoad FileUplodModel = new FileLoad();
foreach var item in doc.File)
{
byte [] uploadFile = new 字节 [item.InputStream.Length];
item.InputStream.Read(uploadFile, 0 ,uploadFile.Length);
FileUplodModel.FileName = item.FileName;
FileUplodModel.File = uploadFile;

}
return FileUplodModel;
}





然后在控制器中应用以下代码



private readonly FileloadBusiness _db = new FileloadBusiness();



public ActionResult Creating()

{

返回查看();

}





[HttpPost]

public ActionResult Creating(FileloadView doc)

{

try

{

_db.Insert(doc); < br $>


返回RedirectToAction(已上传);

}

catch(例外e)

{



返回查看();

}

}



并将其添加到您的视图中

@using(Html.BeginForm(null,null,FormMethod.Post,new {enctype =multipart / form-da ta}))

{

@ Html.AntiForgeryToken()










@ ViewBag.size

@ Html.ValidationSummary(true, ,new {@class =text-danger})



@TempData [successfull]

@ Html.Label(文件名,htmlAttributes:new {@class =control-label col-md-2,style =width:150px})



@ Html.EditorFor(model => model.FileName,new {htmlAttributes = new {@class =form-control}})

@ Html.ValidationMessageFor(model => model.FileName,,new {@class = text-danger})









@ Html.LabelFor(x => x.File)

@ Html.TextBoxFor(x => x.File,new {type =file, multiple =true})

@ Html.ValidationMessageFor(x => x.File)











< input type =submitvalue =Uploadclass =btn btn-default/>







}


http://stackoverflow.com/questions/12086183/how-to-restrict-the-file-types-in-fileupload-in-mvc3 [ ^ ]

Hi i am easily uploading a file of any type but my requiement is to upload a file of .pdf, .doc,.docx extensions.

I have tried but is not taking....Please help me...

解决方案

The following link gives you a working sample: http://stackoverflow.com/questions/4328947/limit-file-format-when-using-input-type-file


I believe this would help

public void Insert(FileloadView doc)
{
    using (var rep = new FileloadRepository())
    {
        rep.Insert(ConvertToDocuments(doc));
    }
}

private static FileLoad ConvertToDocuments(FileloadView doc)
{
    FileLoad FileUplodModel = new FileLoad();
    foreach (var item in doc.File)
    {
        byte [] uploadFile = new byte[item.InputStream.Length];
        item.InputStream.Read(uploadFile, 0, uploadFile.Length);
        FileUplodModel.FileName = item.FileName;
        FileUplodModel.File = uploadFile;

    }
    return FileUplodModel;
}



and then apply the following code in the controller

private readonly FileloadBusiness _db = new FileloadBusiness();

public ActionResult Creating()
{
return View();
}


[HttpPost]
public ActionResult Creating(FileloadView doc)
{
try
{
_db.Insert(doc);

return RedirectToAction("Uploaded");
}
catch (Exception e)
{

return View();
}
}

And add this to your View
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()





@ViewBag.size
@Html.ValidationSummary(true, "", new { @class = "text-danger" })


@TempData["successfull"]
@Html.Label("File Name", htmlAttributes: new { @class = "control-label col-md-2", style = "width: 150px" })


@Html.EditorFor(model => model.FileName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FileName, "", new { @class = "text-danger" })





@Html.LabelFor(x => x.File)
@Html.TextBoxFor(x => x.File, new { type = "file", multiple = "true" })
@Html.ValidationMessageFor(x => x.File)






<input type="submit" value="Upload" class="btn btn-default" />




}


http://stackoverflow.com/questions/12086183/how-to-restrict-the-file-types-in-fileupload-in-mvc3[^]


这篇关于如何在mvc中上传.pdf,.docx和.doc的文件文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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