Server Side Code To Save File From HTML5 File Input -


i'm lost in writing server side code save file html5 file input. have seen examples in php cannot use due ict restrictions , have working example in aspx, post file existing asmx web service, cannot figure out how write server side code. have been struggling on week.

i'm thinking trying post html5 form data aspx page , upload way, don't think that's best solution. have thoughts on efficient route upload file server isn't php or aspx?

<div id="filearea" class="row desc">                     <div class="col-xs-12">                         <form id="uploadform" class="form-inline" action=javascript:uploadfile() method="post" enctype="multipart/form-data">                             <div class="form-group">                                                                     <input type="file" class="form-control" id="filetoupload" name="filetoupload" onchange=javascript:fileselected() placeholder="upload attachment" aria-describedby="filehelp">                             </div>                             <button type="submit" class="btn btn-primary">upload file</button>                             <small id="filehelp" class="form-text text-muted">only files extensions "xls, xlsx, doc, docx, pdf, jpeg, png, msg" permitted</small>                         </form>                     </div>                 </div> 

<script>     function uploadfile() {                 var xhttp = new xmlhttprequest();         var url = "http://localhost:57766/palwebservice.asmx/uploadfile"         var = document.getelementsbyname("existingguid")[0].value;         var fileinput = document.getelementbyid('filetouplaod').files[0];         var filename = fileinput.name                     var b = 'filename=' + filename + '&fileinput=' + fileinput;                 xhttp.open("post", url, true);          xhttp.setrequestheader("content-type", "multipart/form-data");                      xhttp.onreadystatechange = function () {             if (this.readystate == 4 && this.status == 200) {                 alert(xhttp.responsetext);             }         };          xhttp.send(b);                 }     </script> 

//asp form im calling in iframe in html page  <form id="form1" runat="server" class="form-inline"> <div class="form-group">     <asp:fileupload cssclass="form-control" id="fileupload1" runat="server" />      <asp:button class="btn btn-primary" id="button1" runat="server" onclick="button1_click" text="upload" />     <br />     </div>     <asp:label id="label1" runat="server"></asp:label>             </form>  

//aspx.cs save file server path public partial class _default : system.web.ui.page { protected void page_load(object sender, eventargs e) {  } protected void button1_click(object sender, eventargs e) {     bool correctextension = false;      if (fileupload1.hasfile)     {         //string filename = fileupload1.postedfile.filename;          string filename = "turnstile_data.txt";          int filesize = fileupload1.postedfile.contentlength;         string fileextension = path.getextension(filename).tolower();                     string[] extensionsallowed = { ".doc", ".docx", ".xls", ".xlsx", ".txt", ".jpg", ".png" };          if (extensionsallowed.contains(fileextension))         {             correctextension = true;         }          if (correctextension)         {             if (fileextension == ".docx" || fileextension == ".txt" || fileextension == ".xlsx")             {                 try                 {                     string filepath = "\\\\usatl01mt281\\data\\test\\" + filename;                                             fileupload1.postedfile.saveas(filepath);                     //savefilepath(filename, filesize, filepath);                     label1.text = "file uploaded , file path saved in database";                  }                 catch (exception ex)                 {                     label1.text = "unable upload file";                 }             }             else if (fileextension == ".jpg" || fileextension == ".png")             {                 try                 {                     string filepath = server.mappath("~/files/images/" + filename);                     fileupload1.postedfile.saveas(filepath);                     //savefilepath(filename, filesize, filepath);                     label1.text = "file uploaded , file path saved in database";                 }                 catch (exception ex)                 {                     label1.text = "unable upload file";                 }             }          }         else         {             label1.text = "file extension " + fileextension + " not allowed";         }     } }    

}


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -