如何从ASP.NET MVC页面将文件保存到用户的计算机? [英] How can I save a file to the user's computer from an ASP.NET MVC page?

查看:82
本文介绍了如何从ASP.NET MVC页面将文件保存到用户的计算机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些Winforms代码迁移到ASP.NET MVC,这一行:



I am trying to migrate some Winforms code to ASP.NET MVC, and this line:

File.WriteAllBytes(pdfFile, bytes);





...将无法编译,告诉我,'System.Web.Mvc.Controller .File(byte [],string)'是'方法',在给定的上下文中无效



是否有可能完成可以使用Winforms应用程序在ASP.NET MVC中执行相同的操作(将文件写入磁盘)?我认为这是一个不允许这样的安全问题,但是可以将文件下载到客户端,所以我假设可以通过将字节写入流来完成类似的操作......但是如何,确切地说?



如果这不可行/可能,我想我会将字节存储在数据库表中,然后通过链接从那里获取文件。



我尝试过的事情:



我摸不着头脑,但除了这个想法外没有任何东西掉下来关于将数据保存到表中。



...won't compile, telling me, "'System.Web.Mvc.Controller.File(byte[], string)' is a 'method', which is not valid in the given context"

Is it possible to accomplish the same thing (write a file to disk) in ASP.NET MVC as can be done with a Winforms app? I'm assuming it's a security issue that disallows this, but it is possible to download files to the client, so I assume something similar can be done by writing the bytes to a stream...but how, exactly?

If this is not feasible/possible, I reckon I will store the bytes in a database table and then make the file available from there via a link.

What I have tried:

I scratched my head, but nothing fell out, except the idea about saving the data to a table.

推荐答案

您无法在用户(客户端)的计算机上保存任何内容,但用户可以保存所有内容。



你得到了答案;而不是想一想:在另一个Universe中,Web应用程序可以保存开发人员在客户端计算机上想要的任何东西,有多少人想使用这种类型的Internet? :-)



现在,你得到的错误信息中有什么不清楚的地方?查看 System.Web.Mvc.Controller.File

Controller.File Method (System.Web.Mvc) [ ^ ]。



这是一种方法。将。*添加到方法名称时会出现问题。我不认为它需要任何解释。



显然,你搞砸了 System.IO.File

文件类(System.IO) [ ^ ]。



当然它的方法 WriteAllBytes 只能在服务器端保存任何东西,以及任何其他方法。







但这并不意味着您无法将任何文件传递到客户端站点。当然可以;这是一件很重要的事情。实际上,所有服务器端技术的目的都是为了做到这一点 - 动态生成一些HTTP响应并传输任何数据。



但它可以做到只有在一些HTTP请求上,你可以通过显而易见的方式实际生成一些页面。为简单起见,我们假设您创建了一些锚点,因此用户单击它以接收文件。一个简单的方法是在服务器端创建一个文件,并在该锚点上提供一个链接。但是,显然,这不是你想要的。该链接可以是一个ASP.NET代码,可以动态生成HTTP响应。您可以根据某些数据生成内容,创建适当的标题,例如application / zip或application / json(媒体类型 [ ^ ]),在HTTP响应的网络流中写入内容:

HttpResponse Class(System.Web) [ ^ ],

HttpResponse.ContentType属性(System.Web) [< a href =https://msdn.microsoft.com/en-us/library/system.web.httpresponse.contenttype%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ]

HttpResponse.ContentEncoding属性(System.Web) [ ^ ],

HttpResponse.OutputStream属性(System.Web) [ ^ ]。



[结束编辑]



我能说什么?对不起,但是通过这种理解,如语法,错误消息,命名空间和其他非常基本和简单的东西,以及没有Web的基本概念,很难想象任何有效的ASP.NET MVC开发。听取一个很好的友好建议:首先学习语言,.NET和一般编程的基础知识;为此,编程最简单的控制台应用程序将是最好的。



我真的无法理解它。 16篇具有传奇身份的文章,其中很多都是关于ASP.NET的...这个帖子很难通过一点点随机错误来解释;错误消息在一秒钟内被识别。你想让我们神秘吗?



-SA
You cannot save anything on the user's (client's) computer, but the user can save everything.

You got the answer; and not think for a second: in an alternative Universe, where Web application would be able to save anything the developer wants on clients' computers, how many would like to use such kind of Internet? :-)

Now, what is unclear in the error message you get? Look at System.Web.Mvc.Controller.File:
Controller.File Method (System.Web.Mvc)[^].

This is a method. The trouble starts when you add .* to the method's name. I don't think it needs any explanation.

Apparently, you messed it up with System.IO.File:
File Class (System.IO)[^].

And of course its method WriteAllBytes could save anything only on the server side, as well as any other methods.



But it does not mean you cannot deliver any file to the client site. Of course you can; and this is an elementary thing. Actually, the whole purpose of all server-side technology is to do it — to generate some HTTP response on the fly and transmit any data.

But it can be done only on some HTTP request, which you can actually generate of some page, by obvious means. For simplicity, let's assume you create some anchor, so the user clicks on it to receive the file. A trivial way is to create a file on a server side and provide a link on it in that anchor. But, apparently, this is not what you want. The link could be an ASP.NET code which generates HTTP response on the fly. You generate content based on some data, create appropriate header, such as "application/zip" or "application/json" (Media Types[^]), write content in the network stream of the HTTP response:
HttpResponse Class (System.Web)[^],
HttpResponse.ContentType Property (System.Web)[^],
HttpResponse.ContentEncoding Property (System.Web)[^],
HttpResponse.OutputStream Property (System.Web)[^].

[END EDIT]

What can I say? Sorry, but with this level of understanding such thins as syntax, error message, namespaces and other very basic and simple things, and also without the basic idea of what Web does, it's hard to imagine any productive ASP.NET MVC development. Listen to a good friendly advice: learn he basics of language, .NET and general programming first; for this purpose, programming of simplest console-only applications would be the best.

I really cannot understand it. 16 articles with "Legend" status, many of them are on ASP.NET… This post is hard to explain by a little random mistake; the error message is recognized in a second. Are you trying to mystify us?

—SA


这篇关于如何从ASP.NET MVC页面将文件保存到用户的计算机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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