. NET uploading and parsing of CSV file repositories

Source: Internet
Author: User
Tags parse csv file

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.