I have a small problem. My case is that I am inserting some data into my Data Table, I need to export them into csv file. I could be able to write the code but I have a problem that the location of saving must be static. Is there anyway , same as WPF or
Windows Forms, in ASP.net to let the user open save dialog and choose the path and even write the name?
I think first you can generate the cvs file and save as a temporary file in your server-side temporary file folder .
Tranfmit the file as attchment . while you download the file , then you can choose your save path and write the name.
Assume you got a button when user click the button do this:
Response.ContentType = "Application/vnd.ms-excel";// these code write in the button click event
Response.AppendHeader("Content-Disposition", "attachment; filename=data.cvs");
Response.TransmitFile(Server.MapPath("~/temp_files/data.cvs"));//here server.mappath is your saved temp file[data.cvs]
Response.End();
Many thanks for your answer, below is what I currently have:
when the user click on Save button, WrtieCSV function will be called but here the path is a static :(
string path = "F:\\MigrationFrom.csv";
CreateCSVFile(MigrationResult, path); // MigrationResult is my data table and after filling it with data using Adapter
public void CreateCSVFile(DataTable dt, string strFilePath){ // the Funtion
}
I need the path to be determined by a user as well as file name !
so, you may give me another way, a better way of doing this :) especially that I also need an output in Differant formats i.e : xls, csv, .dbf or others.
Here I also deal with around 2,000,000 records from Oracle database tables.
I think you can add a dropdownlist or combox control(item like:excel,word,pdf,dbf etc), then user can select the type of file, and add a textbox, user can write the name of the file (also path). in the code behind , when user click the button, you can get
the value of dropdownlist and textbox, then you can dynamic set the type and the path of the file that you wanna save(here i got a question for you : do you really want to save the created file in server side? you got so many records!).
Of course I DONT :), and for that I asked how can i let the user determine the path. it's not applicable to let the user write a path into a text box ! Come On!!! what I need is like when we browse to save a file, same as we want to download sth from any
page. We usually select the path, then write the name.
For security , based on my exerpience and research user can browse the server side file system. and Unfortunately my knowledge about save file in server side is incomplete, so what i can suggest you is : after the file been trasmit to user side then delete
it.
Base on my exerpience and research, you can create a save file folder in your Project, then user can save file into the folder. It is static path. And as far as I know, web application don't have something like WF Save File Dialog.
private void DownloadFile(bool forceDownload)
{
string type = "";
// set known types based on file extension
if (ext != null)
{
switch (ext.ToLower())
{
case ".htm":
case ".html":
type = "text/HTML";
break;
case ".txt":
type = "text/plain";
break;
case ".doc":
case ".rtf":
type = "Application/msword";
break;
case ".xls":
type = "Application/x-msexcel";
break;
}
}
if (forceDownload)
{
Response.AppendHeader("content-disposition",
"attachment; filename=" + name);
}
if (type != "")
Response.ContentType = type;
Response.AddHeader("Content-Disposition", "attachment; filename=" );
Response.WriteFile(path);
Response.TransmitFile(strRolloverFilepath);
Response.End();
}
Wael Abu Rez...
Member
53 Points
40 Posts
Save File Dialog into Web Based Application
Apr 17, 2012 05:33 PM|LINK
Dear team :),
I have a small problem. My case is that I am inserting some data into my Data Table, I need to export them into csv file. I could be able to write the code but I have a problem that the location of saving must be static. Is there anyway , same as WPF or Windows Forms, in ASP.net to let the user open save dialog and choose the path and even write the name?
Many thanks
Vipindas
Contributor
5514 Points
810 Posts
Re: Save File Dialog into Web Based Application
Apr 17, 2012 05:40 PM|LINK
Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("content-disposition", "attachment; filename=test.csv"); Response.TransmitFile(Server.MapPath("~/Uploads/test.csv")); Response.End();Mark-yu
Participant
888 Points
127 Posts
Re: Save File Dialog into Web Based Application
Apr 19, 2012 06:12 AM|LINK
Hi
I think first you can generate the cvs file and save as a temporary file in your server-side temporary file folder .
Tranfmit the file as attchment . while you download the file , then you can choose your save path and write the name.
Assume you got a button when user click the button do this:
Response.ContentType = "Application/vnd.ms-excel";// these code write in the button click event Response.AppendHeader("Content-Disposition", "attachment; filename=data.cvs"); Response.TransmitFile(Server.MapPath("~/temp_files/data.cvs"));//here server.mappath is your saved temp file[data.cvs] Response.End();hope it hellps. if not , please let me know.
Wael Abu Rez...
Member
53 Points
40 Posts
Re: Save File Dialog into Web Based Application
Apr 20, 2012 07:44 AM|LINK
Many thanks for your answer, below is what I currently have:
when the user click on Save button, WrtieCSV function will be called but here the path is a static :(
string path = "F:\\MigrationFrom.csv";
CreateCSVFile(MigrationResult, path); // MigrationResult is my data table and after filling it with data using Adapter
public void CreateCSVFile(DataTable dt, string strFilePath){ // the Funtion
}
I need the path to be determined by a user as well as file name !
so, you may give me another way, a better way of doing this :) especially that I also need an output in Differant formats i.e : xls, csv, .dbf or others.
Here I also deal with around 2,000,000 records from Oracle database tables.
Many thanks again
Mark-yu
Participant
888 Points
127 Posts
Re: Save File Dialog into Web Based Application
Apr 20, 2012 01:36 PM|LINK
Hi,
I think you can add a dropdownlist or combox control(item like:excel,word,pdf,dbf etc), then user can select the type of file, and add a textbox, user can write the name of the file (also path). in the code behind , when user click the button, you can get the value of dropdownlist and textbox, then you can dynamic set the type and the path of the file that you wanna save(here i got a question for you : do you really want to save the created file in server side? you got so many records!).
Wael Abu Rez...
Member
53 Points
40 Posts
Re: Save File Dialog into Web Based Application
Apr 20, 2012 01:52 PM|LINK
Of course I DONT :), and for that I asked how can i let the user determine the path. it's not applicable to let the user write a path into a text box ! Come On!!! what I need is like when we browse to save a file, same as we want to download sth from any page. We usually select the path, then write the name.
Mark-yu
Participant
888 Points
127 Posts
Re: Save File Dialog into Web Based Application
Apr 20, 2012 02:26 PM|LINK
Hi,
For security , based on my exerpience and research user can browse the server side file system. and Unfortunately my knowledge about save file in server side is incomplete, so what i can suggest you is : after the file been trasmit to user side then delete it.
thanks
Wael Abu Rez...
Member
53 Points
40 Posts
Re: Save File Dialog into Web Based Application
Apr 20, 2012 02:30 PM|LINK
mmm, so you suggest that I let the user save the file into server, then transmit the file into a location where can choose.
but I am really wondering ! I only need , like WPF or Windows Forms, sth such as Save file Dialog where I can get the path and even the name !
and I have a datatable with write function to write the data into csv file.
what do you think ?
Mark-yu
Participant
888 Points
127 Posts
Re: Save File Dialog into Web Based Application
Apr 24, 2012 03:54 AM|LINK
Hi,
Base on my exerpience and research, you can create a save file folder in your Project, then user can save file into the folder. It is static path. And as far as I know, web application don't have something like WF Save File Dialog.
ManojNR
Member
114 Points
59 Posts
Re: Save File Dialog into Web Based Application
Apr 24, 2012 04:56 AM|LINK
private void DownloadFile(bool forceDownload) { string type = ""; // set known types based on file extension if (ext != null) { switch (ext.ToLower()) { case ".htm": case ".html": type = "text/HTML"; break; case ".txt": type = "text/plain"; break; case ".doc": case ".rtf": type = "Application/msword"; break; case ".xls": type = "Application/x-msexcel"; break; } } if (forceDownload) { Response.AppendHeader("content-disposition", "attachment; filename=" + name); } if (type != "") Response.ContentType = type; Response.AddHeader("Content-Disposition", "attachment; filename=" ); Response.WriteFile(path); Response.TransmitFile(strRolloverFilepath); Response.End(); }