?
Solved

Download File using v.net

Posted on 2013-12-14
7
Medium Priority
?
295 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 2000 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
Technology Partners: 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses

777 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