从服务器下载CSV [英] Downloading CSV from the server

查看:99
本文介绍了从服务器下载CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我已经创建了一个网页,我需要下载csv文件,我已经创建了这样的,并且它在我的开发机器中工作正常,当我在现场部署它时,它无法正常工作。任何机构都可以提供建议。



我尝试过:



public FileResult NYUDownload(string Id)

{

try

{

String sFileName = Id.ToString();

字符串outPutFilePath = Server.MapPath(〜/ App_Data / CSV)+\\+ Id +。csv;

MemoryStream mem = new MemoryStream(System.IO.File.ReadAllBytes(outPutFilePath));

返回文件(mem,application / vnd.ms-excel,sFileName +。csv);

}

catch(例外情况)

{

返回null;

}

}

Hi i have created a web page, where i need to download the csv file, i have created like this and its working fine in my developing machine, when i am deploying that in the live, its not working. Can any body advise.

What I have tried:

public FileResult NYUDownload(string Id)
{
try
{
String sFileName = Id.ToString();
String outPutFilePath = Server.MapPath("~/App_Data/CSV") + "\\" + Id + ".csv";
MemoryStream mem = new MemoryStream(System.IO.File.ReadAllBytes(outPutFilePath));
return File(mem, "application/vnd.ms-excel", sFileName + ".csv");
}
catch (Exception ex)
{
return null;
}
}

推荐答案

没有下载任何内容:它从磁盘读取文件并返回它以适合以二进制数据形式传递给客户端的形式提供给您自己的代码: FileResult Class (System.Web.Mvc) [ ^ ]

不会自动发送它:你需要看一下确切地说,一旦你返回,你正在对数据做什么来自NYUDownload方法。



但是......如果一切都在dev中工作但不在生产中,那么你可能会尝试将其直接保存到磁盘,似乎工作,因为在dev中Server和Cli9ent是同一台计算机。在现实世界中它们不是 - 并且由于C#代码仅在服务器上运行,因此您无法使用它将文件数据保存到客户端HDD。由于非常好的原因,您的服务器代码绝对无法直接访问客户端硬件!
That doesn't download anything: it reads a file from the disk and returns it to your own code in a form that should be suitable for passing to the client as binary data: FileResult Class (System.Web.Mvc)[^]
That does not automatically send it: you need to look at exactly what you are doing with the data once you return from the NYUDownload method.

But ... if it all works in dev but not in production, then you are probably trying to save it to disk directly, which would appear to work because in dev the Server and the Cli9ent are the same computer. In the "real world" they aren't - and since C# code only runs on the Server you can't use it to save the file data to the Client HDD. For very, very good reasons, your Server code has absolutely no direct access to the Client hardware!


这篇关于从服务器下载CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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