Link to home
Start Free TrialLog in
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
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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shahjagat

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
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.
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
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
Thanks