. Net Method for reading client files

Source: Internet
Author: User

Previously, it was written to C/S, and there was basically no need to consider the issue of program permissions. Now I am writing B/s and want to have an interaction between the program and the client. All of a sudden, I am worried that I will always report an error and have no permission! If you want to perform this operation directly, it won't work! Finally, I can only give up.

For example, to operate EXCEL files on the client, there is a good workbook in JAVA, which is used to store the EXCEL files uploaded on the client (memory) and does not need to be written to the hard disk, but there is a problem: memory occupation (temporary, large files are obvious), operations are still performed on the server (the client does not have the permission ).. Net is first stored (written to the hard disk) and then written to the memory.

Each has its own advantages and disadvantages, but JAVA provides another way!

 

If the client program reads text files, all the methods are available. You can read the uploaded content directly from the memory or store it on the hard disk before reading it!

Browser code:

 1 

Background code:

The reading of text files provides a direct way to read from the memory. EXCEL adopts the method of storing the files to the server and then reading them!

Using System; using System. collections. generic; // using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; using System. data; using System. data. oleDb; using System. IO; public partial class _ Default: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {} protected void button#click (object sender, EventArgs e) {string fileName = this. getUploadFileCont Ent. fileName; // file name string filePath = this. getUploadFileContent. postedFile. fileName; // complete path + file name string fileType = System. IO. path. getExtension (fileName); if (fileType = ". txt ") {int fileLength = this. getUploadFileContent. postedFile. contentLength; byte [] input = new byte [fileLength]; System. IO. stream fileStream = this. getUploadFileContent. postedFile. inputStream; fileStream. read (input, 0, fileLeng Th); fileStream. position = 0; System. IO. streamReader sr = new System. IO. streamReader (fileStream, System. text. encoding. default); this. textBox1.Text = sr. readToEnd (); fileStream. close (); sr. close ();} if (fileType = ". xls ") {string savePath = Server. mapPath ("~ \ Upfile \ ") + fileName; if (File. exists (savePath) {File. delete (savePath); this. getUploadFileContent. saveAs (savePath);} else {this. getUploadFileContent. saveAs (savePath);} if (File. exists (savePath) {DataSet ds = new DataSet (); string str = "Provider = Microsoft. jet. OLEDB.4.0; Data Source = "+ savePath +"; Extended Properties = \ "Excel 8.0; HDR = NO; IMEX = 1 \" "; OleDbConnection conn = new OleDbConnection (str ); string strSql = "select * from [Sheet1 $]"; OleDbDataAdapter adp = new OleDbDataAdapter (strSql, conn); conn. open (); adp. fill (ds); if (ds. tables [0]. rows. count> 0) {for (int I = 0; I <ds. tables [0]. rows. count; I ++) {this. textBox1.Text + = ds. tables [0]. rows [I] [0]. toString () + "\ r \ n" ;}} conn. close (); File. delete (savePath );}}}}

You will understand a lot later! Learn more!

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.