During the summer vacation, a pocketpc project involves uploading and downloading files, including text, images, and other things. It is implemented using WebServices and has written the following two methods: 1 Public Static Void Stringtofile ( String Sfilepath, String Sstring)
2 {
3 Filestream OUTFILE;
4 Byte [] Binarydata;
5 Binarydata = System. Convert. frombase64string (sstring );
6 OUTFILE = New Filestream (sfilepath, filemode. Create, fileaccess. Write );
7 OUTFILE. Write (binarydata, 0 , Binarydata. Length );
8 OUTFILE. Close ();
9 }
10
11 Public Static String Filetostring ( String Sfilepath)
12 {
13 Filestream infile;
14 Byte [] Binarydata;
15 Infile = New Filestream (sfilepath, filemode. Open, fileaccess. Read );
16 Binarydata = New Byte [infile. Length];
17 Long Bytesread = Infile. Read (binarydata, 0 ,( Int ) Infile. Length );
18 Infile. Close ();
19 Return System. Convert. tobase64string (binarydata, 0 , Binarydata. Length );
20 }
The second method: filetostring (string sfilepath)
Parameter: file path
Purpose: encode the object into a string in base64 format.
The first method is stringtofile (string sfilepath, string sstring)
Parameter: the first parameter indicates the file path to be stored.
The second parameter is a base64 string encoded using the preceding method.
In this way, we can upload and download files in WebServices.