• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

Download File using v.net

Does anyone have working VB.net code that will download a file when a button is clicked.
When a user connects to a web page there is a button that they click that will then download an excel file and ask the user where they want to save the file. I am not using windows form I am using vb.net with ajax controls. I have been trying to get this to work for sometime now. Pleas help
0
whiwex
Asked:
whiwex
  • 5
1 Solution
 
käµfm³d 👽Commented:
Basically, you can create a new page that does nothing other than return the file:

Markup
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Download.aspx.vb" Inherits="_28318331.Download" %>

Open in new window


Code-behind
Public Class Download
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim fileData() As Byte = System.IO.File.ReadAllBytes(Server.MapPath("~/Content/Files/sample.xls"))

        Response.Clear()
        Response.Headers("Content-Type") = "application/vnd.ms-excel"
        Response.Headers("Content-Disposition") = "attachment; filename=""sample.xls"""
        Response.BinaryWrite(fileData)
        Response.Flush()
        Response.End()
    End Sub

End Class

Open in new window


Then, simply point your button to that URL:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="ExamplePage.aspx.vb" Inherits="_28318331.ExamplePage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button PostBackUrl="~/Download.aspx" runat="server" />
    </div>
    </form>
</body>
</html>

Open in new window


AJAX should work as well. You might need to fiddle with the Content-Type and Content-Disposition headers.

Screenshot
0
 
whiwexAuthor Commented:
This is almost working. The spreadsheet now opens but none of the menu items work.
0
 
whiwexAuthor Commented:
I have to use the browser back arrow to go back to the previous page.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
whiwexAuthor Commented:
if I click refresh page in the browser the menu items work
0
 
whiwexAuthor Commented:
Actually non of the click envents work after the spreadsheet page is loaded.
0
 
CodeCruiserCommented:
I think Excel has disabled much of the functionality as the file has been downloaded from the internet. It shows an "Enable" button under the toolbar which you need to click to enable functionality.
0
 
whiwexAuthor Commented:
Thanks Just had to change the button from postbackurl to navigateurl
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now