Asked by:
Save as dialog box

Question
-
User746671234 posted
I've a button to Export file to Excel. On this button I've written the code to generate excel file now in the end I have to show the dialog box to show the file and to save file in client. How could I do this from code behind. I'm using ASP.NET
Currently I'm saving it using hard code like
Dim excelFilePath As String = "d:\test.xls" Dim fi As FileInfo = New FileInfo(excelFilePath) excelPackage.SaveAs(fi)
I want to show save as dialog and user will select the folder to save.
Saturday, May 22, 2021 2:08 PM
All replies
-
User475983607 posted
If I understand correctly, your requirement is not possible in a web application since the code is running on the web server. Also, how will the user know where to save the file on the remote server?
If you expect the user to save the file on their system then allow the user to download the file.
Saturday, May 22, 2021 2:20 PM -
User746671234 posted
If I understand correctly, your requirement is not possible in a web application since the code is running on the web server. Also, how will the user know where to save the file on the remote server?
If you expect the user to save the file on their system then allow the user to download the file.
Yes I have to allow user to download the file in client machine. But then I have to show another box with download file option. I have to directly show save as dialog box to save file locally.
Saturday, May 22, 2021 2:25 PM -
User475983607 postedYes I have to allow user to download the file in client machine. But then I have to show another box with download file option. I have to directly show save as dialog box to save file locally.
The browser controls the save dialog which you have no control over. The best you can do is set the content disposition header.
https://weblog.west-wind.com/posts/2007/may/21/downloading-a-file-with-a-save-as-dialog-in-aspnet
Keep in mind the code you've shared saves the file on the web server.
Saturday, May 22, 2021 2:31 PM -
User746671234 posted
mgebhard
ravininave
Yes I have to allow user to download the file in client machine. But then I have to show another box with download file option. I have to directly show save as dialog box to save file locally.The browser controls the save dialog which you have no control over. The best you can do is set the content disposition header.
https://weblog.west-wind.com/posts/2007/may/21/downloading-a-file-with-a-save-as-dialog-in-aspnet
Keep in mind the code you've shared saves the file on the web server.
Tried like this but it's not working
Dim filename = "DailySales" + Format(DateTime.Now, "ddMMyyyyHHmmss") + ".xlsx" Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename.ToString) Response.TransmitFile(filename.ToString) Response.End()
I just wish to open a save as box and get the file path from the user.
Saturday, May 22, 2021 2:39 PM -
User475983607 posted
Did you read the link??? You have no control over the save prompt.
Saturday, May 22, 2021 2:47 PM -
User746671234 posted
Yes, I have copied the whole code.
Did you find any problem in my code?
Saturday, May 22, 2021 3:02 PM -
User475983607 postedDid you find any problem in my code?
There is nothing wrong with the code. It is not possible to force the browser to show the save as dialog.
Saturday, May 22, 2021 5:15 PM -
User746671234 posted
Alright is there any way to open excel file directly without saving it. Below is my code where I am saving the file as xls. I don't want to save it directly instead I am thinking to open it directly and then if user needs he will save it.
Dim filename = "test" + Format(DateTime.Now, "ddMMyyyyHHmmss") + ".xlsx" Response.Clear() excelPackage.SaveAs(Response.OutputStream) Response.AddHeader("content-disposition", "attachment; filename=" + filename + ";") Response.Charset = "" Response.ContentType = "application/vnd.xlsx" Response.End()
Wednesday, May 26, 2021 10:09 AM -
User475983607 posted
ravininave
Alright is there any way to open excel file directly without saving it. Below is my code where I am saving the file as xls. I don't want to save it directly instead I am thinking to open it directly and then if user needs he will save it.Your requirement is not possible and assumes every client has Excel installed. Anyway, remove the line below...
Response.AddHeader("content-disposition", "attachment; filename=" + filename + ";")
or set Content-Disposition to inline.
Response.AddHeader("content-disposition", "inline; filename=" + filename + ";")
Keep in mind, this question is related to HTTP and how browser work not Web Forms.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
Wednesday, May 26, 2021 10:31 AM