Solved

Download File using v.net

Posted on 2013-12-14
7
275 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Encryption Decryption in Oracle 12 107
Multiple file Upload asp.net 2 40
Enable copy and pasting even when textbox is disabled 2 26
Get String split 5 48
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now