简体   繁体   中英

How to Write file in C#?

 string attachment = "attachment; filename=Myfile.csv";
 HttpContext.Current.Response.Clear();
 HttpContext.Current.Response.ClearHeaders();
 HttpContext.Current.Response.ClearContent();
 HttpContext.Current.Response.AddHeader("content-disposition", attachment);
 HttpContext.Current.Response.ContentType = "text/csv";
 HttpContext.Current.Response.AddHeader("Pragma", "public");

// My logic goes here to write file.

// Now I want to copy above file to another file.

 string filepath = Server.MapPath("~/ExportedFiles/") +"Newfile.csv";
 var ExcelFile = File.Create(filepath);
 ExcelFile.Close();
 HttpContext.Current.Response.TransmitFile(filepath);
 HttpContext.Current.Response.Flush();

The above code is not working, please suggest me. Thanks.

you need to create a virtual directory on your IIS and share that virtual directory directory. Your csv should reside in your virtual directory shared location. here is referenced article: http://www.codeproject.com/Tips/502741/A-solution-to-problem-Response-TransmitFile

use code bellow rather than: var ExcelFile = File.Create(filepath); WriteAllLines instruction in C# use to create file or change content file

File.WriteAllLines(filepath, data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM