JavaScript - Save as dialog - Asked By Lokesh M on 09-Dec-08 02:46 AM

I would like to open save as image dialog box when i click on the hyperlink in both IE and firefox.. i'm using below code.. but it is working only in IE but not in Firefox.. any suggestion / alternative please help me..

<script>

 function saveImageAs (imgOrURL) {
    if (typeof imgOrURL == 'object')
      imgOrURL = imgOrURL.src;
    window.win = open (imgOrURL);
    setTimeout('win.document.execCommand("SaveAs")', 500);
  }
</script>
<body>

  <A HREF="javascript: void 0"
     ONCLICK="saveImageAs(document.anImage); return false"
  >save image</A>
  <IMG NAME="anImage" SRC="base_x.gif">
</body>

reply - Perry replied to Lokesh M on 09-Dec-08 02:52 AM

You need the server return the headers content-type: application/octet-stream. To do this, You most likely do not want to do a generic change in the server config

    AddType application/octet-stream .txt

instead use a server process (perl, php, .net, jsp) to set the headers and instead have

<a href="servefile.php?file=otherpage.txt">Save text file</a>


or Simply use this

<a href="otherpage.txt" onClick="alert('Please use right mouse and save-as'); return false">Other page - use right-click</a>

the above javascrpt will - Venkat K replied to Lokesh M on 09-Dec-08 02:56 AM

work's fine in only IE4.0 or later it won't work for Firefox. You can write asp.net code to make it work in both IE and Firefox

see this article how to implement that in asp.net

http://www.west-wind.com/weblog/posts/76293.aspx

Or if possible try this javascript and check whether it is working in Firefox or not

function ShowSaveComplete() {

if (document.all) {

var OLECMDID_SAVEAS = 4;

var OLECMDEXECOPT_DONTPROMPTUSER = 2;

var OLECMDEXECOPT_PROMPTUSER = 1;

var WebBrowser = "<OBJECT ID=\"WebBrowser1\" WIDTH=0 HEIGHT=0 CLASSID=\"CLSID:8856F961-340A-11D0-A96B-00C04FD705A2\"></OBJECT>";

document.body.insertAdjacentHTML("beforeEnd", WebBrowser);

WebBrowser1.ExecWB(OLECMDID_SAVEAS, OLECMDEXECOPT_PROMPTUSER);

WebBrowser1.outerHTML = "";

} else {

alert("This is only applicable to Internet Explorer");

}

}

use ful - selva murugan replied to Lokesh M on 09-Dec-08 02:57 AM

<a href="#" onclick="javascript: document.execCommand('SaveAs','1','saveMe.csv');"><img src="./images/save.gif" border="0"></a>

this code will be default save the document as 'saveMe.csv'. You can even define a full qualified path. Explanation in the link http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/methods/execcommand.asp
Save as dialog - mv ark replied to Lokesh M on 09-Dec-08 03:38 AM
document.execCommand("SaveAs") does not work in Firefox because it is not a Standard method - http://msdn.microsoft.com/en-us/library/ms536419(VS.85).aspx

The functionality you desire is not available possibly because it is a security issue.

This may be possible by manipulating the Content-Disposition header, but it's support maybe flaky in some browsers. Check this link for details - http://www.jtricks.com/bits/content_disposition.html

try this code - C_A P replied to Lokesh M on 09-Dec-08 06:01 AM
You would need to use the execCommand function...
Like...

<button onclick="javascript:document.execCommand('SaveAs', '1','');">Click to save</Button>
try this - C_A P replied to Lokesh M on 09-Dec-08 06:02 AM
Imports System.IO
Imports System.Web.HttpContext

Public Class Downloader

    Private mPath As String

    Public Property Path() As String
        Get
            Return mPath
        End Get
        Set(ByVal value As String)
            mPath = value
        End Set
    End Property

    Public Sub Save()
        Dim strPath As FileInfo
        strPath = New FileInfo(mPath)
        Current.Response.Clear()
        Current.Response.AddHeader("Content-Disposition", "attachment; filename=" & strPath.Name)
        Current.Response.AddHeader("Content-Length", strPath.Length.ToString())
        Current.Response.ContentType = "application/octet-stream"
        Current.Response.WriteFile(strPath.FullName)
        Current.Response.End()
    End Sub

End Class
try this - C_A P replied to Lokesh M on 09-Dec-08 06:03 AM

See the following code ::

</head>
<script>

 function saveImageAs (imgOrURL) {
    if (typeof imgOrURL == 'object')
      imgOrURL = imgOrURL.src;
    window.win = open (imgOrURL);
    setTimeout('win.document.execCommand("SaveAs")', 500);
  }
</script>
<body>

  <A HREF="javascript: void 0"
     ONCLICK="saveImageAs(document.anImage); return false"
  >save image</A>
  <IMG NAME="anImage" SRC="../apache_pb2.gif">
</body>

See the following link for more details about this code :: http://forums.digitalpoint.com/showthread.php?t=702678

Hope it helps.