A few days ago, in order to solve cross-origin file interaction and upload files. Converts a file to binary for transmission, facilitating storage.
First, there is a problem when uploading files, that is, the desktop files on the computer and files on some disks have permission control and access is prohibited.
Later I checked and found that there were a lot of posts about setting up a computer. As a web program, it was too troublesome to set up a computer.
It turns out that the IIS port cannot be around 100.
To solve the problem, you can set it in webconfig. Add the following code and you will be OK.
<System. web> <br/> <identity impersonate = "true" username = "username" Password = "password"/> <br/> <authorization> <br/> <allow users = "? "/> <! -- Deny indicates disabled, users = "? "Allow indicates that anonymous users are allowed <br/> --> <br/> </authorization> <br/> </system. Web>
The following is the code for saving and downloading files.
String Path = @ "C:/Documents and Settings/Administrator/desktop/new documents .txt"; // physical file path <br/> int fcount = 0; <br/> string contenttype = ""; <br/> string filename = ""; </P> <p> fileinfo Fi = new fileinfo (PATH ); // obtain the File Information <br/> fcount = (INT) Fi. length; <br/> contenttype = Fi. extension; <br/> filename = Fi. fullname. substring (Fi. fullname. lastindexof ('//') + 1, Fi. fullname. lastindexof ('. ')-fi. fullname. lastin Dexof ('//')-1); <br/> filestream FS = new filestream (path, filemode. open, fileaccess. read); // read the file <br/> binaryreader BR = new binaryreader (FS); <br/> byte [] photo = BR. readbytes (fcount); <br/> Br. close (); <br/> FS. close (); </P> <p> session ["test"] = photo; // Save the binary file to the session </P> <p> byte [] by = (byte []) session ["test"]; // obtain the session information <br/> filename = setting. tohexstring (filename); // transcode the object to prevent Chinese garbled characters.. <Br/> response. addheader ("content-disposition", "attachment; filename =" + filename + contenttype + ""); // set the file header to be read <br/> response. addheader ("Content-Length",. length. tostring (); <br/> response. contenttype = "application/octet-stream"; // set the output type, which can be saved in the database for dynamic implementation <br/> response. outputstream. write (by, 0,. length); // output <br/> response. flush (); <br/> response. end ();
The setting. tohexstring (filename) method solves the problem of Chinese garbled characters in the file stream. The following method is not original on the Internet.
I would like to thank the author for sharing it with you.
/// <Summary> <br/> // encode the non-English characters in the string <br/> /// </Summary> <br/> // <Param name = "S"> </param> <br/> // <returns> </returns> <br/> Public static string tohexstring (string S) <br/> {<br/> char [] chars = S. tochararray (); <br/> stringbuilder builder = new stringbuilder (); <br/> for (INT Index = 0; index <chars. length; index ++) <br/>{< br/> bool needtoencode = needtoencode (chars [Index]); <br/> If (ne Edtoencode) <br/>{< br/> string encodedstring = tohexstring (chars [Index]); <br/> builder. append (encodedstring); <br/>}< br/> else <br/>{< br/> builder. append (chars [Index]); <br/>}</P> <p> return builder. tostring (); <br/>}</P> <p> // <summary> <br/> // specify whether a character should be encoded. <br/> // </Summary> <br/> /// <Param name = "CHR"> </param> <br/> /// <returns> </returns> <br/> private Static bool needtoe Ncode (char CHR) <br/>{< br/> string reservedchars = "$-_. +! * '(), @ = & "; </P> <p> If (CHR> 127) <br/> return true; <br/> If (char. isletterordigit (CHR) | reservedchars. indexof (CHR)> = 0) <br/> return false; </P> <p> return true; <br/>}</P> <p> /// <summary> <br/> // It is not an English string encoded <br/> /// </Summary> <br/> // <Param name = "CHR"> </param> <br/> // <returns> </returns> <br/> Private Static string tohexstring (char CHR) <br/>{< br/> utf8encoding utf8 = new utf8encoding (); <br/> byte [] encodedbytes = utf8.getbytes (CHR. tostring (); <br/> stringbuilder builder = new stringbuilder (); <br/> for (INT Index = 0; index <encodedbytes. length; index ++) <br/>{< br/> builder. appendformat ("% {0}", convert. tostring (encodedbytes [Index], 16); <br/>}< br/> return builder. tostring (); <br/>}
Displays Chinese garbled characters that have not been processed.
Display after processing
Well, I hope you will have more exchanges.