jquery - ftpwebrequest with progress bar -
i'm creating asp.net mvc 5 application, users should able upload large files (2-3 gb). upload files, make use of ftpwebrequest class. right now, i've small jquery script makes ajax call, sends complete path server, , there controller uploads file external ftp server.
i want add progress bar. below can find i've got far:
- on button click modal opens;
- uploading starts (even when close ie, continues);
- the process bar not working.
what doing wrong?
index.cshtml
@{ viewbag.title = "file upload"; } <div class="row"> <h2>file upload</h2> </div> <div class="row"> <div class="form-group"> <input type="file" name="file" id="file" /> </div> <div class="form-group noleftpadding"> <input type="button" value="upload file" class="btn btn-primary" /> </div> </div> @section scripts{ <script type="text/javascript"> var myapp; myapp = myapp || (function () { var modal = ''; modal += '<div class="modal" id="pleasewaitdialog" data-backdrop="static" data-keyboard="false">'; modal += '<div class="modal-dialog">'; modal += '<div class="modal-content">'; modal += '<div class="modal-header"><h1>processing...</h1></div>'; modal += '<div class="modal-body"><div class="progress progress-striped active"><div class="bar" style="width: 100%;"></div>'; modal += '</div></div></div>'; var pleasewaitdiv = $(modal); return { showpleasewait: function () { pleasewaitdiv.modal(); }, hidepleasewait: function () { pleasewaitdiv.modal('hide'); }, }; })(); $(function () { $(":button").on("click", function () { uploadfile(); }); function uploadfile() { myapp.showpleasewait(); var file = $('#file').val(); alert(file); var formdata = new formdata(); formdata.append("file", file); ajax = new xmlhttprequest(); ajax.upload.addeventlistener("progress", progresshandler, false); ajax.addeventlistener("load", completehandler, false); ajax.open("post", "/upload/ajaxuploadfile/"); ajax.send(formdata); } function progresshandler(event) { var percent = (event.loaded / event.total) * 100; $('.bar').width(percent); } function completehandler() { myapp.hidepleasewait(); $('.bar').width(100); } }); </script> }
uploadcontroller
public actionresult ajaxuploadfile(string file) { dictionary<string, string> strmsg = new dictionary<string, string>(); completepath = system.io.path.getfullpath(file); filename = system.io.path.getfilename(completepath); string strftp = "ftp://" + serverip + "/folder/" + filename; try { ftpwebrequest ftpreq = (ftpwebrequest)webrequest.create(strftp); ftpreq.usebinary = true; ftpreq.method = webrequestmethods.ftp.uploadfile; ftpreq.credentials = new networkcredential(username, password); byte[] b = system.io.file.readallbytes(completepath); ftpreq.contentlength = b.length; using (stream s = ftpreq.getrequeststream()) { s.write(b, 0, b.length); } ftpwebresponse ftpresp = (ftpwebresponse)ftpreq.getresponse(); if (ftpresp != null) { strmsg.add("fileupload", "success!"); } else { strmsg.add("fileupload", "no file uploaded!"); } return json(strmsg, jsonrequestbehavior.allowget); } catch (exception ex) { strmsg.add("fileupload", "server error!"); return json(strmsg, jsonrequestbehavior.allowget); } }
Comments
Post a Comment