Download image(s) from the web

You can use the method HTTPDownload() or HTTPDownloadEx() to download an image from the web server into Dynamic Web TWAIN.

DWObject.HTTPDownload("www.dynamsoft.com","/images/dwt-logo.png", optionalAsyncSuccessFunc, optionalAsyncFailureFunc);
/* Callback functions for async APIs */
function optionalAsyncSuccessFunc (){
    console.log('successful');
}
function optionalAsyncFailureFunc (errorCode, errorString){
    alert(errorString);
}

This is especially useful when you want to review an image created and uploaded by Dynamic Web TWAIN. Even when the image data is stored in the database, you can write an action page to pull the data from the database and get it downloaded (in this case, you need to use the method HTTPDownloadEx because the image format needs to be specified explicitly). Besides the HTTP download methods, you can also use the FTP download methods to download image(s) from an FTP server. Available methods are FTPDownload, FTPDownloadEx, etc.

NOTE:Same as mentioned earlier in the section Uploading image(s) to the web server, special configuration has to be made on the server to overcome browser security restrictions. When you try to download an image from a server with a different domain, subdomain, port, or protocol, you need to configure your server to allow such requests by adding one HTTP Response Header, namely:

Access-Control-Allow-Origin: *

Take IIS 7 for example, what you need to do is merge the following lines into the web.config file at the root of your application / site:

<?xml version="1.0" encoding="utf-8"?>
 
<configuration>    
  <system.webServer>      
    <httpProtocol>        
      <customHeaders>          
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET,PUT"/>
        <add name="Access-Control-Allow-Headers" value="x-requested-with"/>
        <add name="Access-Control-Allow-Credentials" value="true" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

If you don't have a web.config file already, just create a new file called "web.config" and add the snippet above.

Is this page helpful?

Leave a Reply

Your email address will not be published.