Avatar of shahjagat
shahjagatFlag for United States of America

asked on 

Save File dialog web application

Hi,

I am working on a web application using VS2008.(VB.NET)

My scenario:
I want to export data  to exce lfro mmy datatable.
I have the code to export to excel.(This works with a fixed path like "C:\Users\Desktop\myfile.csv")

What i want:
User should be able to choose where he wants to save this file.(like in save file dialog in winforms.)

How can i do this?

Thanks
ASP.NETVisual Basic.NETVisual C++.NET

Avatar of undefined
Last Comment
shahjagat
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

You can't - remember that your VB.Net code runs on the server, not on the computer that's using your web application. So when you use VB.Net code to save a file to"C:\Users\Desktop\myfile.csv" you're saving that file on the server's hard drive, not the desktop of the user viewing the website.

All you can do is direct the users' web browser to the file to be downloaded; what prompts, if any, that are shown to the user are entirely up to his web browser and you have no control over that.
Hi,
Please try following code:

Sub SaveFile()
        Dim sdg As New SaveFileDialog()
        If (sdg.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            Dim fileName As String = sdg.FileName ' It will give you filename and you can use it to create file
            ' Your logic to save file
        End If

    End Sub

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of shahjagat
shahjagat
Flag of United States of America image

ASKER

Hi Tgerbert,

My scenario is like this.

If 2 persons  A and B save data.

A should not access data of B and B should not see data of A.

Probably i have to export the file to a folder named after userid and give the maccess to that folder.


How would you handle sucha situation:
Each user exporting data to excel which is differnt and  confidential.
If i export it only i should be able to see it. No one else.

Thanks
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

I would export it to a folder in your web application (but which is not accessible via a URL) with a temporary file name.  Then redirect the user to another page, e.g. an ASHX, which will use code-behind to read the temporary file and send it to the client.
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Or you can directly write the gridview to the response and use the xls mimetype to force browser to show the open/save dialogbox

http://www.c-sharpcorner.com/uploadfile/dipalchoksi/exportxl_asp2_dc11032006003657am/exportxl_asp2_dc.aspx
Avatar of shahjagat
shahjagat
Flag of United States of America image

ASKER

Hi,

Folowing code worked for me. Took bits and pieces from different sources.


http://www.west-wind.com/weblog/posts/2007/May/21/Downloading-a-File-with-a-Save-As-Dialog-in-ASPNET


 Private Sub ExportToExcel()
        'Create the CSV file to which grid data will be exported.
        Dim userid As String = Nothing
        userid = Session("Username")
        Dim fnm As String = Nothing
        'Dim mydate As String = Now.Date
        'mydate = mydate.Replace("/", "")
        Dim filename As String = Nothing
        Dim path As String = Nothing

        Dim vpath As String = Server.MapPath("\Reports")


        path = vpath
        Directory.CreateDirectory(path)
        filename = userid

        'fnm = "TripBilling" & userid & mydate & ".csv"
        fnm = "TripBilling" & userid & ".csv"
        path = path & "\" & fnm
        Dim sw As New StreamWriter(path)


        If Not Session("ReportData") Is Nothing Then
            mydt = Session("ReportData")
            Dim iColCount As Integer = mydt.Columns.Count
            For i As Integer = 0 To iColCount - 1
                sw.Write(mydt.Columns(i))
                If i < iColCount - 1 Then
                    sw.Write(",")
                End If
            Next
            sw.Write(sw.NewLine)

            For Each dr As DataRow In mydt.Rows
                For i As Integer = 0 To iColCount - 1
                    If Not Convert.IsDBNull(dr(i)) Then
                        sw.Write(dr(i).ToString())
                    End If
                    If i < iColCount - 1 Then
                        sw.Write(",")
                    End If
                Next
                sw.Write(sw.NewLine)
            Next
            sw.Close()
        Else

        End If


        Response.ContentType = "Text/csv"
        Response.AppendHeader("Content-Disposition", "attachment; filename=" & fnm)
        Response.TransmitFile(path)
        Response.End()


    End Sub
Avatar of shahjagat
shahjagat
Flag of United States of America image

ASKER

Thanks
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo