shahjagat
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.c sv")
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
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.c
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
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
Please try following code:
Sub SaveFile()
Dim sdg As New SaveFileDialog()
If (sdg.ShowDialog() = Windows.Forms.DialogResult
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
If you are using asp.net then review following links:
http://www.codeproject.com/KB/custom-controls/Excel_CSV_export_button.aspx
http://support.microsoft.com/kb/317719
http://www.codeproject.com/KB/custom-controls/Excel_CSV_export_button.aspx
http://support.microsoft.com/kb/317719
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
http://www.c-sharpcorner.com/uploadfile/dipalchoksi/exportxl_asp2_dc11032006003657am/exportxl_asp2_dc.aspx
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("Con tent-Dispo sition", "attachment; filename=" & fnm)
Response.TransmitFile(path )
Response.End()
End Sub
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(
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("Con
Response.TransmitFile(path
Response.End()
End Sub
ASKER
Thanks
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.