Transfer files with Web service (ii)

Source: Internet
Author: User
Tags base64 file size file upload ftp soap file transfer protocol

More topics

Today is July 20, 2002. One weeks ago, I rolled out this article and got some friends of the corresponding. They have raised a number of questions and I would like to reply by continuing this article.

Implement file Upload

Friends ask me, your sample code only files from the server download to the client's introduction, is not implied that the Web Service can not implement files from the client to the server? No, this is my negligence, should have mentioned "file upload" Problem!

In fact, the so-called "file upload" is very simple, but is "file download" Reverse thinking! The client reads the file and converts it into a BASE64 encoded XML string as an argument to the calling server about the interface function; The server simply decodes the XML string from the client and saves it as a file!

Server-side code

FUNCTION SaveFile (suser as string,spassword as string,sfilename as string,sxml as String) as Boolean
IF suser<> "Admin" OR spassword<> "PassWord" Then
return. F.
ENDIF

IF Strtofile (STRCONV (sxml,14), databasepath+sfilename) >0 Then
Return. T.
ELSE
return. F.
ENDIF
Endfunc

The core code here is: Strtofile (STRCONV (sxml,14), databasepath+sfilename). Its function is: The parameter sXml carries on the Base64 decoding, and saves the decoded data as the file.

In addition, some netizens mentioned is not a user authentication problem, here I use the most simplified code to explain:

IF suser<> "Admin" OR spassword<> "PassWord" Then
return. F.
ENDIF

This interface function can only be used when the user name (Suser) is "Admin" and the password (spassword) is "PassWord". Otherwise, the function will return directly without proceeding to the following!

We imagine that this simple code can actually expand into a complex "user, Permissions" system, this is just the general concept of the program design, here is not much to say!

Client's Code

Complete code, as detailed in SAVEFILE.PRG

#define TempPath "J:\webs\ClientData\"
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")
IF Mywebs. SaveFile ("Admin", "PassWord", "Pic.jpg", STRCONV (Filetostr (temppath+ "yl_22_28.jpg"), 13)
MESSAGEBOX ("Success!") ")
ELSE
MESSAGEBOX ("Failed!") ")
ENDIF

The client's code is more simple, the core is: Mywebs. SaveFile ("Admin", "PassWord", "Pic.jpg", STRCONV (filetostr "temppath+"), 13). Its function is reads the JPG file yl_22_28.jpg, and carries on the BASE64 code to it, finally takes the SaveFile interface function The SXML parameter to send the server!

For this application, my idea is to replace the calendar file with the calendar from July 22, 2002 to July 28!

A simple comparison between Web Service transfer files and FTP

The Web Service uses the Http protocol "client request, server-side response" mode, which belongs to the synchronization mechanism! That is, after the client invokes the server interface function, it waits until the server gives the result (or timeout)! The advantage of this pattern is that the client, the server interacts as we call the internal function in visual FoxPro-waiting for the return result, which is great for programming! Personal feeling, with FTP control, interactivity without Web Service good!

Note that when we write a Web Service with Visual FoxPro, we can write a lot of logical code. In other words, when using the Web Service to transfer files, the server is more customized, whether upload, download files, our server can complete the complex custom logic code! Of course, if you are interested in writing FTP Server, the same can be done!

Say so much, in the final analysis: The Web Service is not designed to transfer files, FTP is born to pass files! Maybe someone's going to hit me, and we've been working on it for 2 weeks, in return for the phrase: Web Service is not designed to transfer files.

It's important to note that Web Service transfer files are a by-product of this technology, not a design-time starting point! The function invocation pattern of WEB service is especially suitable for the realization of business logic. FTP is a file transfer protocol, is widely used, nature has its advantages, such as two-file direct transmission of documents, such as the continuation of the breakpoint!

Therefore, in peacetime applications, if the file size is not large, and the need to transfer files in the Web Service application, you should use here we introduce the method: the use of Web service transfer files! If it is just an orphan file transfer task or the file is very large, you should use FTP or Email!

The size of the file is not a deterministic. My personal opinion is the key to seeing the transmission time, if a 1M file is to be transmitted for 10 minutes, I think 1M is a large file for this environment, conversely, if a 5M file, as long as the 30 seconds to pass the completion, then in this environment it is only a small file!

Further, the length of this waiting time, is to make your own decision!

Calling Web Service asynchronously

Here, someone asked: My Web service provides interface functions for a long time, can not implement the client call, do not wait to return results and continue to work on other tasks!

This is obviously a desire to get an "asynchronous processing" Environment! I've just mentioned that Web service works in "synchronous mode," and more specifically: Web service that does not work asynchronously, only asynchronous calls to Web service! Obviously, the crux of the problem is on the client side!

In Visual FoxPro How to implement an asynchronous call to a Web Service, I don't have a clue yet! But there are two ways to do this in. NET: The callback function and the WaitHandle () function. The former is suitable for WinForm, and the latter is suitable for asp.net.

Microsoft Soap Toolkit 3

A few days ago, Ms introduced Soap Toolkit 3, and in this version, MS has special support for file delivery. If you are interested, you can study: http://msdn.microsoft.com/downloads/default.asp? Url=/downloads/sample.asp?url=/msdn-files/027/001/948/msdncompositedoc.xml

Visual FoxPro 7 uses SOAP Toolkit 2 by default, and the code in this article runs in more than 2 versions of SOAP Toolkit!

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.