1. Front End:
Place browse button
<class= "Row inner_table text-center"> < ID = "FileId" type = "File" name = "Filestring" class= "file"> </ Div >
2. JS:
Registering the Browse button event
varChosefile =function () { $("Input[name=filestring]"). Change (function () { varFilePath = $ ("[name= ' filestring ']"); //Read File contents varFormData =NewFormData (); Formdata.append (' file[] ', filepath[0].files[0]); $.ajax ({URL:"Xxxxxxxx Inbound Link", type:' POST ', Data:formdata, async:false, Cache:false, ContentType:false, ProcessData:false, Success:function(returndata) {alert ("Upload succeeded"); }, Error:function(returndata) {alert ("Upload Failed"); } }); });}
3 Controller:
[Customauthorize (authtype.authrozied)] Publicjsonresult dataimport (formcollection form) {stringZipdir = Path.Combine (AppDomain.CurrentDomain.BaseDirectory,"Templatefiles"); stringMasterfile = Path.Combine (Zipdir,"Template"+". csv"); if(!directory.exists (Zipdir)) {directory.createdirectory (Zipdir); } varFile = request.files[0];
Upload file. SaveAs (Masterfile); DataTable DT=Getcsvtodata (Masterfile); List<ScheduleTableInfo> infos =NewList<scheduletableinfo>(); foreach(DataRow DataRowinchdt. Rows) {Scheduletableinfo info=NewScheduletableinfo (); Info. TemplateName= datarow["Title Name 1"]. ToString (); Info. Sumlevelname= datarow["Title Name 2"]. ToString (); Infos. ADD (info); } //Performing Inbound Operations intValue =_client. ImportData (infos); varJsonstr = Jsonconvert.serializeobject (infos,NewDatatableconverter (),NewJavascriptdatetimeconverter ()); Jsonstr= Jsonstr.replace ("\\\\n","\\r"); varresult =Json (Jsonstr, jsonrequestbehavior.allowget); returnresult; }
Function sub-function:
PrivateDataTable Getcsvtodata (stringStrcsvpath,BOOLHasheader =true,BOOLNeedlineno =false) {Csvreader Csvreader=NULL; Try{Csvreader=NewCsvreader (Strcsvpath, Fileencoding.gettype (Strcsvpath)); DataTable DT= Csvreader.gettable (1, -1, Hasheader); returnDT; } Catch(Exception ex) {Logservice.debug ("Parse CSV file error:"+ex); return NULL; } finally { if(Csvreader! =NULL) {csvreader.dispose (); } } }
Import file, get file location
. NET uploading and parsing of CSV file repositories