Saturday, 15 September 2012

asp.net - Restrict file type to be upload -



asp.net - Restrict file type to be upload -

i have deleted before question regarding file upload using classic asp. have switched .net accomplish goal still unable restrict file type i.e. pdf & docx beingness upload.

code behind under:

using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.io; public partial class cs : system.web.ui.page { protected void page_load(object sender, eventargs e) { if (!ispostback) { string[] filepaths = directory.getfiles(server.mappath("~/upload/")); list<listitem> files = new list<listitem>(); foreach (string filepath in filepaths) { files.add(new listitem(path.getfilename(filepath), filepath)); } gridview1.datasource = files; gridview1.databind(); } } protected void uploadfile(object sender, eventargs e) { string filename = path.getfilename(fileupload1.postedfile.filename); fileupload1.postedfile.saveas(server.mappath("~/upload/") + filename); response.redirect(request.url.absoluteuri); } protected void downloadfile(object sender, eventargs e) { string filepath = (sender linkbutton).commandargument; response.contenttype = contenttype; response.appendheader("content-disposition", "attachment; filename=" + path.getfilename(filepath)); response.writefile(filepath); response.end(); } protected void deletefile(object sender, eventargs e) { string filepath = (sender linkbutton).commandargument; file.delete(filepath); response.redirect(request.url.absoluteuri); } }

html page under:

<%@ page language="c#" autoeventwireup="true" codefile="safetyupload.aspx.cs" inherits="cs" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:fileupload id="fileupload1" runat="server" /> <asp:button id="btnupload" runat="server" text="upload" onclick="uploadfile" /> <hr /> <asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" emptydatatext = "no files uploaded"> <columns> <asp:boundfield datafield="text" headertext="file name" /> <asp:templatefield> <itemtemplate> <asp:linkbutton id="lnkdownload" text = "download" commandargument = '<%# eval("value") %>' runat="server" onclick = "downloadfile"></asp:linkbutton> </itemtemplate> </asp:templatefield> <asp:templatefield> <itemtemplate> <asp:linkbutton id = "lnkdelete" text = "delete" commandargument = '<%# eval("value") %>' runat = "server" onclick = "deletefile" /> </itemtemplate> </asp:templatefield> </columns> </asp:gridview> </form> </body> </html>

i have tried no avail

protected void uploadfile(object sender, eventargs e) { if (fileupload1.hasfile) { seek { if (fileupload.postedfile.contenttype == "pdf") { string filename = path.getfilename(fileupload1.filename); fileupload1.postedfile.saveas(server.mappath("~/upload/") + filename); response.redirect(request.url.absoluteuri); } else label1.text = "pdf files only"; } grab (exceptionex) { label1.text = "error during uploading file"; } } }

please suggest solutions.

use path.getextension can have like

string fileextension = path.getextension(filename); fileextension = fileextension.tolower(); string[] acceptedfiletypes = { ".docx", ".pdf" }; bool acceptfile = false; (int = 0; <= 1; i++) { if (fileextension == acceptedfiletypes[i]) { acceptfile = true; } } if (!acceptfile) { label1.text = "you error message here"; return; }

asp.net file-upload file-type

No comments:

Post a Comment