Solved

Download File using v.net

Posted on 2013-12-14
7
270 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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
This is almost working. The spreadsheet now opens but none of the menu items work.
0
 

Author Comment

by:whiwex
Comment Utility
I have to use the browser back arrow to go back to the previous page.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:whiwex
Comment Utility
if I click refresh page in the browser the menu items work
0
 

Author Comment

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

Expert Comment

by:CodeCruiser
Comment Utility
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
Comment Utility
Thanks Just had to change the button from postbackurl to navigateurl
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

11 Experts available now in Live!

Get 1:1 Help Now