asp.net upload execl file, load the display on the page

Source: Internet
Author: User
Tags file size file upload int size

  This article is mainly on the ASP.net upload execl file, loaded on the page display (sample code) was introduced, the need for friends can come to the reference, I hope to help you.

  Code as follows:     #region upload execl file     protected void Button1_Click (object sender, EventArgs e)     {        if (fileupload1.hasfile)         {        & nbsp   String newfilename = string. Empty;             String errormess = UploadFile (FileUpload1, ". xls|. Xlsx ", 1024 * 5, Server.MapPath ("/report/socialapply/"), 1, out newfilename);             if (string. IsNullOrEmpty (errormess))             {              &NB Sp Label1.Text = "√ File upload success";                 viewstate["uploadfile"] = "/report/socialapply/" + newfilename;                 try                 {&nbs P                   FileStream file = new fileStream                         (Server.MapPath viewstate["Uplo Adfile "] As String),                         FileMode.Open, Fi Leaccess.read,                         fileshare.readwrite);                     DataTable dt1 = FairHR.Util.XmlExcelReport.ReadExcel Todatatable (file, 0, 0);                       Viewstate.add ("Viewdt", DT1);                     Gridview1.datasource = viewstate["VIEWDT"] as Datata ble                     gridview1.databind ();                     file. Close ();                    //maticsOft. Common.MessageBox.ResponseScript (Page, "$.messager.alert" (' System hint ', ' Operation successful!) ', ' info ');                                 Catch &N Bsp               {                    M Aticsoft. Common.MessageBox.ResponseScript (Page, "$.messager.alert" (' System hint ', ' please upload execl file again ', ' warning ');                            }             Else             {              &N Bsp Label1.Text = "x file upload failed";                     {        else     & nbsp   {            Label1.Text = "x Please select upload file First";        }         SCRIPTMANAGER.REgisterclientscriptblock (Page, this.) GetType (), "IdAlert1", "closeload ();", true);    }     #endregion       #region uploading files    ///<summary>     Upload files    ///</summary>    ///<param name= "fu" > upload file </param>    /// <param name= "type" > upload file type, such as. Jpg|. Gif|. bmp</param>    ///<param name= "size" > Limit upload file size, Unit k</param>    ///<param Name= "path" > upload path, need to use server.mappath</param>    ///<param name= "NameType" >1 for automatic naming, 0 used formerly </ param>    ///<returns></returns>     public static string UploadFile (FileUpload fu, S Tring type, int size, string path, int nametype, out string newfilename)     {        NEWFI Lename = null;         string erorr = null;         int Size = Fu. postedfile.contentlength/1024;         if (Size > size)         {            Erorr = "Upload file is too large ! ";             return Erorr;        }         string Type = Fu. FileName;         if (Type.indexof (".") = = 1) {Erorr = "Upload file type is wrong!" "; return Erorr; }         Type = type.substring (Type.lastindexof (".")). ToUpper ();         type = type. ToUpper ();         if (type. IndexOf (type) = = 1) {Erorr = "Upload file type is wrong!" "; return Erorr; }           string filename = "";         if (nametype = 1)         {            STR ing nowdate = DateTime.Now.ToString ();             nowdate = nowdate. Replace (":", ""). Replace ("", ""). Replace ("-", ""). Trim ();             Random r = new Random (); &nbsp           int a = R.next (1000);             filename = nowdate + a.tostring () + Type;             newfilename = filename;        }         else         {        & nbsp   filename = fu. FileName;             if (System.IO.File.Exists (path + filename)) {Erorr = "This file name already exists!" "; return Erorr; }        }           FU. SaveAs (path + filename);         return erorr;//error message returned, no error returned null     {    #endregion  
Related Article

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.