Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

run func and save file prompt 1

Status
Not open for further replies.

thompom

Technical User
Dec 4, 2006
395
GB
Hi,

I need to run a function and prompt to save a file in one click - at the moment i have

Code:
<a href="files/file.xxx" onclick="sndHit('1');return false"><img src="images/dowloadicon.gif" alt="Download file" border="0" /></a>

but the link doesnt prompt to save the file after the onclick event was added.
i think i need a save file dialog called from sndHit function?

 
Hi

The browser will decide it the save file dialog is needed. It depends on the browser's capabilities and the settings. Generally, is none of any site's webmaster's business what happens on the visitors computer.

Anyway, if you want more comments on this, you should provide more information :
[ul]
[li]Why you need the [tt]onclick[/tt] event handler ?[/li]
[li]What happens in the sndHit() function ?[/li]
[li]If is related to the file specified in the [tt]href[/tt], what is its MIME type ?[/li]
[/ul]


Feherke.
 
hi - thanks for reply [on a saturday ;-)]

1) onclick event runs this AJAX script to increase the download counter

Code:
 var http = createRequestObjectHIT();
 function createRequestObjectHIT() 
     {
           var xmlhttp;
	 try 
                 { 
                    xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
                 }
	  catch(e) 
                 {
	    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	    catch(f) { xmlhttp=null; }
	    }
	        if(!xmlhttp&&typeof XMLHttpRequest!="undefined") 
                        {
	  	   xmlhttp=new XMLHttpRequest();
	           }
		   return  xmlhttp;
 }
function sndHit(idnum) 
  {
            try
              {
                 http.open('GET', 'hitprocess.asp?id='+idnum);
                 http.onreadystatechange = handleResponseTextHIT;
	    http.send(null);
	 }
	    catch(e){}
	    finally{}
 }
function handleResponseTextHIT() 
  {
     try
         {
             if((http.readyState == 4)&& (http.status == 200))
                {
    	          var response = http.responseText;
                       var update = new Array();

                    if(response.indexOf('|') != -1) 
                       {
                          update = response.split('|');
                          var grnelement = document.getElementById('grn'+update[0]);
                          grnelement.innerHTML = update[1];
             }
	        }
        }
	catch(e){alert("an error occured");}
	finally{}
	
}

2) there is no mime type as it is a custom file type just for download
 
Hi

thompom said:
1) onclick event runs this AJAX script to increase the download counter
Got it. But still abit strange for me. Some possible ways :
[ul]
[li]Do not rely on JavaScript, count the downloads on server-side. The best is to extract the information from the web server's access log, there is mentioned the file size too.[/li]
[li]Do not break the link's default behavior. The AJAX request will be sent in the background.
Code:
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"files/file.xxx"[/i][/green] [maroon]onclick[/maroon][teal]=[/teal][green][i]"sndHit('1')"[/i][/green][b]><img[/b] [maroon]src[/maroon][teal]=[/teal][green][i]"images/dowloadicon.gif"[/i][/green] [maroon]alt[/maroon][teal]=[/teal][green][i]"Download file"[/i][/green] [maroon]border[/maroon][teal]=[/teal][green][i]"0"[/i][/green] [b]/></a>[/b]
[/li]
[li]Send the download request from the script.
Code:
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"files/file.xxx"[/i][/green] [maroon]onclick[/maroon][teal]=[/teal][green][i]"sndHit('1');return false"[/i][/green] [maroon]id[/maroon][teal]=[/teal][green][i]"dl"[/i][/green][b]><img[/b] [maroon]src[/maroon][teal]=[/teal][green][i]"images/dowloadicon.gif"[/i][/green] [maroon]alt[/maroon][teal]=[/teal][green][i]"Download file"[/i][/green] [maroon]border[/maroon][teal]=[/teal][green][i]"0"[/i][/green] [b]/></a>[/b]
Code:
[b]function[/b] [COLOR=darkgoldenrod]handleResponseTextHIT[/color][teal]()[/teal]
[teal]{[/teal]
  [b]try[/b] [teal]{[/teal]
    [gray]// ...[/gray]
    location[teal].[/teal]href[teal]=[/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'dl'[/i][/green][teal]).[/teal]href
  [teal]}[/teal] [b]catch[/b][teal]([/teal]e[teal])[/teal] [teal]{[/teal] [COLOR=darkgoldenrod]alert[/color][teal]([/teal][green][i]"an error occured"[/i][/green][teal]);[/teal] [teal]}[/teal]
[teal]}[/teal]
[/li]
[/ul]
thompom said:
2) there is no mime type as it is a custom file type just for download
There still need to be something specified in the HTTP headers when the file is delivered. Usually there are some steps to set the [tt]Content-type[/tt] for a response :
[ul]
[li]specified by a server-side script[/li]
[li]added by the web server, based on the file extension[/li]
[li]added by the web server, based on the file content[/li]
[li]added by the web server, the configured default[/li]
[/ul]
Is rare and invalid if the content is still delivered without specifying anything.


Feherke.
 
hi - removing the 'return false' works fine
thanks v. much

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top