Solved

Download File using v.net

Posted on 2013-12-14
7
291 Views
Last Modified: 2013-12-17
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
Comment
Question by:whiwex
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39719078
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
 

Author Comment

by:whiwex
ID: 39719287
This is almost working. The spreadsheet now opens but none of the menu items work.
0
 

Author Comment

by:whiwex
ID: 39719334
I have to use the browser back arrow to go back to the previous page.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:whiwex
ID: 39719414
if I click refresh page in the browser the menu items work
0
 

Author Comment

by:whiwex
ID: 39719428
Actually non of the click envents work after the spreadsheet page is loaded.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39721248
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
 

Author Closing Comment

by:whiwex
ID: 39724114
Thanks Just had to change the button from postbackurl to navigateurl
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question