Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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...
Posted

The following link gives you a working sample: http://stackoverflow.com/questions/4328947/limit-file-format-when-using-input-type-file
 
Share this answer
 
I believe this would help

C#
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" />



}
 
Share this answer
 
v2
If the client is using an html5 browser that supports fileapi then you can use that to determine the type, size etc before allowing an upload

http://robertnyman.com/2010/12/16/utilizing-the-html5-file-api-to-choose-upload-preview-and-see-progress-for-multiple-files/[^]

For those without html5 you'll just need to let them upload any file they want and check the extension (or file type) after it has been uploaded to decide if you want to keep the file. Doing the html5 is a nice to have but doing server validation is vital as a malicious user can easily get around your javascript.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900