c# - Using "<input type="file" .... />" instead of asp:FileUpload -
i'm modifying existing asp.net project. original author erroneously tried create styled asp:fileupload setting visibility hidden , creating 2 custom styled browse , save buttons.
for security reason, ie not permit this. strategy instead try use input tags type="file", this example. if set input <input type="file" id="inputfile" /> how access/save file in code behind, inputfile.saveas("somefile.txt");? (in code behind) can inputfile.hasfile or there other analog of this?
as per recommendations i'm trying following:
<td> enabled: <asp:checkbox id="checkbox2" runat="server" /> <div id="testfileuploader">> <input type="file" id="browserhidden" runat="server" /> <div id="browservisible"><input type="text" id="filefield" /></div> </div> </td>
so, can generate random file name future upload, based on guid @ codebehind of aspx page:
httppostedfile fileposted = request.files["uploadfieldnamefromhtml"]; if (fileposted != null && fileposted.contentlength > 0) { string filenameapplication = system.io.path.getfilename(fileposted.filename); string fileextensionapplication = system.io.path.getextension(filenameapplication); // generating random guid new file @ server uploaded file string newfile = guid.newguid().tostring() + fileextensionapplication; // getting valid server path save string filepath = system.io.path.combine(server.mappath("uploads"), newfile); if (filenameapplication != string.empty) { fileposted.saveas(filepath); } } for request.files["uploadfieldnamefromhtml"] set id in html code here:
<input type='file' id='...' /> also, don't forget define runat="server" @ main form in aspx page, it's better set @ main form , don't forget enctype="multipart/form-data" parameter of <form>:
<body> <form enctype="multipart/form-data" id="form1" runat="server"> <input type='file' id='uploadfieldnamefromhtml' /> ...