View Full Version : How to open save as dialog box to save a file on click of button event
manjeet799
07-02-2006, 07:37 PM
Hi,
My application/website required a image to get downloaded on desktop when user clicks save as button.
I need to open the save as dialog box and to save the file at users desired location.
can anybody give me the piece of code or information
regards
otaku149
07-02-2006, 11:11 PM
Hello manjeet799,
Create and place the following Downloader Class into your App_Code directory (ASP.Net 2.0):
Downloader.vb
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
In your .aspx page:
1) Create an instance of the class Downloader:
Dim fileToDownload As New Downloader()
2) Use the Path() property to set the file path:
fileToDownload.Path = Server.MapPath("images/yourimage.jpg")
3) Use the Save() method to open the save as dialog box:
fileToDownload.Save()
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub btDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim fileToDownload As New Downloader()
fileToDownload.Path = Server.MapPath("images/yourimage.jpg")
Try
fileToDownload.Save()
Catch ex As Exception
lblMsg.Text = ex.Message.ToString()
End Try
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Downloader</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btDownload" runat="server" OnClick="btDownload_Click" Text="Download" />
<asp:Label ID="lblMsg" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
manjeet799
07-03-2006, 11:45 AM
Thanks, the code worked fine,
:-)
manjeet799
07-04-2006, 11:54 AM
this works fine for files on local system. I want to download server files. That is I want to give mPath="http://localhost/website/aaa.jpg".
It throws excetion moment it reaches strPath=new FileInfo(mPath)
Please tell me how can I give server path.
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
otaku149
07-04-2006, 01:32 PM
In that case, you must use your server absolute path, something like:
fileToDownload.Path = "C:\wwwroot\website\aaa.jpg"
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.