Transfer files with Web service (i)

Source: Internet
Author: User
Tags base64 functions integer mail zip password protection wsdl

Opening

Last year, when I first wrote my Web Service, I had a question: Could this stuff transfer files!

It's a recurring requirement to write files in real development, and Visual FoxPro programmers do this on the Internet, usually by e-mail or FTP, so can Web Service provide a new way?

Of course, you can use the Web Service to transfer files, which is the subject of our discussion today.

Before we begin, I would like to thank the future for the help of me, the Rambler and Boby in the technical support of me, and dedicate this article to you!!!

Convert a file into text

BASE64 encoding/decoding

I have mentioned many times in previous articles that Web service relies on XML to describe the data, extending to the problem of transferring files, so that you can achieve the goal of using Web service as long as you can represent the transmitted files in XML.

All kinds of files in a computer are basically in binary form, and we know that XML is just the simplest text message. So how to achieve the conversion of arbitrary file and text? This is obviously the crux of the problem!

We will not spare the circle, directly give the solution: BASE64 encoding/decoding! The BASE64 encoding/decoding rule is defined in Rfc2045,multipurpose Internet Mail Extension (MIME). The so-called encoding is: to convert binary data into Base64 (text information), the so-called decoding is to Base64 (text information) to restore the original data!

Note: Email attachments are also BASE64 encoded/decoded!

Base64 is text, which represents information in 64 visible characters, 64 visible characters: A-z,a-z, 0-9, +,/, =.

The implementation of BASE64 encoding/decoding algorithm is not too difficult, because the Visual FoxPro 7 Base64 have specific support, so here I do not talk about the BASE64 algorithm!

Just remember one conclusion: XML can only pass "visible text", we convert any file to text using BASE64 encoding, so we can transmit it in XML. Transmission complete, just want to Base64 decoding into binary data, save as a file on the line!

Yes, there is also a point to be explained: According to the BASE64 encoding algorithm, data conversion to BASE64 after the general volume than the original increase of 1/3! This may be the cost of using Base64 ...

Implementing BASE64 Encoding/decoding in Visual FoxPro

Discover the Secret of StrConv ()

Is there a function in Visual FoxPro that solves the problem of "BASE64 encoding/decoding"? No, you can not find the help! Maybe I'm lucky, maybe I feel good, for this question I have deliberately tested the Visual FoxPro functions that are designed to encode and decode STRCONV (cexpression, nconversionsetting ) The interesting thing is that IntelliSense reveals the secret of the parameters nconversionsetting !

nconversionsetting= 13 o'clock, implement BASE64 encoding function: Convert binary data into Base64 (text information)!

nconversionsetting= 14 o'clock, realize Base64 decoding function: Restore Base64 (text information) to original data!

Do not know why, Microsoft did not put these two parameters to help, at least my help did not mention these two parameters, really overtaxing tian Wu Urumqi!!!

Using the StrConv () function

If you want to convert C:\abc.jpg to BASE64 encoding, you can do this:

Local cBase64 as String
Cbase64=strconv (Filetostr ("C:\abc.jpg"), 13)

If you want to restore the above Base64 encoding to file test.jpg, you can do this:

Strtofile (Strconv (cbase64,14), "c:\test.jpg",. T.)

Is it very easy? is a layer of window paper, it is not unusual to do a bit of it! In fact, if you have read the previous article about making Web service in the Boe, and with today's tips, "How to transfer files with Web service" should be easy for you!

Implementing Web Service

Send a picture

Server-side code

This is a very simple program: the interface function Getpicture returns an XML string bearing the picture to the client program! Here in order to simplify the code, the returned picture is also fixed: pic.jpg.

The protected function Encode64 is able to encode the developed file into Base64, and the core code is:

Return STRCONV (Filetostr (sfilename), 13)

Here are the two functions, specifically see PRG1.PRG file!

PROTECTED FUNCTION Encode64 (sFileName As String) as String
IF FILE (sFileName)
Return STRCONV (Filetostr (sfilename), 13)
ELSE
Return ""
ENDIF
Endfunc

FUNCTION Getpicture as String
return this. Encode64 (databasepath+ "pic.jpg")
Endproc

Client Calls

It is also easy for the client to invoke this Web Service: we use the getpicture () interface to get the XML string that hosts the picture and restore it to test.jpg, and then paste the jpg file into the Visual FoxPro main form!

The code to decode Base64 and save the file here is:

Strtofile (STRCONV) (Mywebs. getpicture,14), temppath+ "Test.jpg",. T.)

The complete code is as follows, specifically see GETPIC.PRG:

#define TempPath "J:\webs\ClientTemp\"
Local Mywebs as Foxwebservice
Local lows
Lows = NewObject ("WSClient", Home () + "FFC\_WEBSERVICES.VCX")
Lows.cwsname = "Foxwebservice"
Mywebs = Lows.setupclient ("Http://BOEWORKS/FoxWebS/FoxWebService.WSDL", "Foxwebservice", "Foxwebservicesoapport")
Strtofile (STRCONV) (Mywebs. getpicture,14), temppath+ "Test.jpg",. T.)
_screen. picture=temppath+ "Test.jpg"

Transmitting DBF files

Introduction of ZIP Technology

We have solved the problem of using Web Service to transfer files, in fact, think about it, you will find that there are a number of details to be accounted for:

    • Some are transmitted to the file (data) is very large, transmitted to the network pressure is very large, and time-consuming. How to make the data smaller, shorten the transmission time, reduce network pressure?

    • How do I do this if I need to transfer more than one file at a time?

My solution is to compress the transferred file in a ZIP format.

Zip to BMP, TXT, DBF and other types of files have a very high compression rate, compressed these files can greatly reduce the amount of data, fast delivery is very beneficial, but also reduce the network data Flow!

Zip also has the ability to wrap multiple files in a zip file, which solves the problem of passing multiple files at once.

In addition, ZIP also supports compression password, there are data mentioned with more than 10-bit length in English and Chinese mixed compression password, generally difficult to crack! Here we compress to add password protection, file transmission security should be good!

Unfortunately, Visual FoxPro does not directly provide zip functionality, and generally we use a third-party dynamic-link library or an ActiveX control to implement the zip. Here I used a dynamic link library called ZipToolKit.dll, which I encapsulated. Features simple, but it should be able to use on the Web Service client and server side should be competent (ZipToolKit.dll is I specially written for this article, so I do not ZipToolKit.dll provide any technical support and warranty!) )。

The use of ZipToolKit.dll

Compress files

function declaration: DECLARE Integer zipfiles in "Ziptoolkit.dll" string,string,integer,string
Return value: 0>=0 The number of successful compressed files;-1 failed
Parameter 1: List of compressed files. The file is represented by a full path, the wildcard character is supported, multiple files are separated by semicolons, and the parameter length is no more than 255 characters.
Parameter 2: File name after compression.
Parameter 3: compression level. A value of 1-9, the larger the value, the higher the compression ratio, the more slowly processing.
Parameter 4: Compresses the password. A blank string indicates that no password is set.

For example:
DECLARE Integer zipfiles in "Ziptoolkit.dll" as Zip string,string,integer,string
? Zip ("F:\zip\abc.bmp; F:\zip\ab.txt "," F:\zip\test.zip ", 9," ")
Clear DLLs "Zip"

Extracting files

function declaration: DECLARE Integer Unzipfile in "Ziptoolkit.dll" string,string,string
Return value: 0>=0 The number of successful uncompressed files;-1 failed
Parameter 1: Uncompressed file
Parameter 2: Release path
Parameter 3: Unzip the password. A blank string indicates that no password is set.

For example:
DECLARE Integer unzipfile in "Ziptoolkit.dll" as unzip string,string,string
? Unzip ("F:\zip\test.zip", "F:\zip\t1t\", "")
Clear DLLs "Unzip"

Why not use Cursortoxml () to deal with the transmission problem of DBF

To get to the point, let's use the WEB service to transfer DBF files. If you are an old reader of the BOE, perhaps you will have the question: DBF is not the Cursortoxml () function can be directly into the XML string, why do we have to find a way to use DBF as a simple file, with a common transmission of files to deal with it?

I think there are two reasons. First, the Cursortoxml () function does not support the conversion of common field data, which means that if a Cursor (DBF) has a common field, the field is ignored by Cursortoxml (), and it is more obvious that Cursortoxml does not support common field types! Encountered such a problem, I personally think that direct transmission of *.dbf and *.FPT files is the easiest way!

DBF as a file transfer there is another reason, when the amount of data is slightly larger, with Cursortoxml () will produce a large XML string (Thousands of data, it may form a few m of XML), if the use of compressed dbf, a very good "thin body" role!

Of course, the cursortoxml () approach we introduced earlier still has its benefits, and it's still the mainstream way of delivering data tables, because when it comes to the internet, we're not advocating for large amounts of data to be passed over the network, but to deliver useful rows and columns; General business processes do not have much chance of dealing with common data, and it is very easy to use the Cursortoxml () function to process tabular data. So don't be "obsessed" with today's topic,!!!.

Server-side code

This is a very simple program: the interface function getdbf returns an XML string bearing the DBF file to the client program (in fact, the BASE64 encoding of the Zip file is transmitted)! Here in order to simplify the code, the returned table file is fixed: EMPLOYEE.DBF and its attached file EMPLOYEE.FPT.

The protected function ZipEncode64 can compress the specified file into a zip file and then encode the zip file as Base64:

Here are the two functions, specifically see PRG1.PRG file!

PROTECTED FUNCTION ZipEncode64 (sfile as string,ilevel as integer,spsd as String) as String
Local stempzipfile,sbase64 as String
Stempzipfile=temppath+sys (2015)
DECLARE Integer zipfiles in "Ziptoolkit.dll" as Zip string,string,integer,string
IF Zip (SFILE,STEMPZIPFILE,ILEVEL,SPSD) >0
Sbase64=this. Encode64 (Stempzipfile)
ELSE
Sbase64= ""
ENDIF
Clear DLLS "ZIP"
IF FILE (Stempzipfile)
DELETE FILE &stempzipfile
ENDIF
Return sBase64
Endfunc

FUNCTION getdbf (Iziplevel as integer,spsd as String) as String
return this. ZipEncode64 (databasepath+ "EMPLOYEE.DBF;") +databasepath+ "EMPLOYEE.FPT", IZIPLEVEL,SPSD)
Endproc

Client Calls

It is also easy for the client to invoke this Web Service: we use the GETDBF () interface to get the XML string that hosts the zip and restore it to Test.zip, then unzip the zip file into the temp directory, get the Employee table file, and finally open and Show employee!

The complete code is as follows, specifically see GETDBF.PRG:

#define TempPath "J:\webs\ClientTemp\"
#define TOOLPATH "J:\webs\tools\"
Local Mywebs as Foxwebservice
Local lows
Lows = NewObject ("WSClient", Home () + "FFC\_WEBSERVICES.VCX")
Lows.cwsname = "Foxwebservice"
Mywebs = Lows.setupclient ("Http://BOEWORKS/FoxWebS/FoxWebService.WSDL", "Foxwebservice", "Foxwebservicesoapport")
SET PATH to Toolpath
IF Select ("Employee") >0
Use in employee
ENDIF
Strtofile (STRCONV) (Mywebs. GETDBF (9, "BOE Data Network Studio"), temppath+ "Test.zip",. T.)
DECLARE Integer unzipfile in "Ziptoolkit.dll" as UnZip string,string,string
IF UnZip (temppath+ "Test.zip", TempPath, "BOE Data Network Studio") >0
Use temppath+ "Employee"
BROWSE
ENDIF
Clear DLLS "Unzip"

and be Together

I don't know what to do with Web Service yet.

If you don't know how to write a Web service server-side program, check the BOE's "write Web Service with Visual FoxPro";

If you do not know how to write a Web Service client, check the BOE's "Visual FoxPro 7 new debut--web Service client";

Download the example how to use the

As usual, basically compiled, published after the use of. Note that every PRG file has predefined variables, usually the path to point to, depending on your situation to change them on the line!

What's the use of Web Service?

If you feel the Web service firsthand, you'll find that calling a Web service on the Internet is as simple as calling a native function. With Web Service, we can interact with all kinds of data, including files!

Perhaps today, Foxer's doubts about Web Service are like those of SQL Server. Be a conscientious person, perhaps you will never use this technology, perhaps tomorrow you will use this technology, understand it is always good, this is my point of view!

Some netizens said I am not pragmatic, always get some imaginary things, anyway, not a few people can understand, the person just. Also do not know to say is right, is wrong, more do not know is copied from there ... This malign slander, do not know these people is what mentality, to this I just want to say: Brother, please give me a quiet sky!!!

All right, here we go! See you next time!

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.