如何从db创建CSV文件并将其保存在MVC中 [英] How to create CSV file from db and save it in MVC

查看:120
本文介绍了如何从db创建CSV文件并将其保存在MVC中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临一个问题。我想从db创建一个csv文件。但我不想下载这个文件。我想把它保存到特定的位置,比如(@D:\\\ output =)。

我是如何做到的。



我尝试了什么:



i am facing a problem. i want to create a csv file from db. but i don;t want to download this file. i want to save this into particular location like (@"D:\output.xml").
how i achieve this.

What I have tried:

var response = System.Web.HttpContext.Current.Response;
            response.BufferOutput = true;

            //clear response
            response.Clear();
            response.ClearHeaders();
            response.ContentEncoding = Encoding.Unicode;

            //For Csv Export give file name
            string filename = string.Format("{0}", DateTime.Now.ToString("g").Replace("/", "-").Replace(":", "_").Replace(" ", "-"));
            response.AddHeader("content-disposition", "attachment;filename=MailMergeLog-" + filename + ".CSV ");
            response.ContentType = "text/plain";
            response.Write(sb.ToString());
            response.End();





但是这段代码直接下载了这个但我想在我的系统中保存这个文件。



but this code directly download this but i want to save this file somewhere in my system.

推荐答案

如果要将文件保存到服务器上的特定位置 ,则需要使用 System.IO 命名空间用于保存文件,而不是发送给客户端。



如果你想控制文件在客户端上保存的地方,那你就不走运了。您无法控制文件保存在客户端的位置,甚至 if 。如果可以,那将是一个主要的安全漏洞。
If you want to save the file to a particular location on the server, then you need to use the classes from the System.IO namespace to save the file, and not send it to the client.

If you want to control where the file is saved on the client, then you're out of luck. You cannot control where, or even if, the file is saved on the client. If you could, that would be a major security vulnerability.


这篇关于如何从db创建CSV文件并将其保存在MVC中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆