如何使用 HTML 将文件上传到网站
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3448411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to upload a file to a website using HTML
提问by vtortola
I'm developing a website and I'd like the users be able to upload files to the server using a webpage.
我正在开发一个网站,我希望用户能够使用网页将文件上传到服务器。
The server side is .NET, but it's not ASP.NET. That means, I'm generating the html code myself instead using ASP.NET, and I'm working with the GET string and POST stream directly. It's a good exercise to learn what happens under the hood :D, specially nowadays when there is a framework for everything.
服务器端是 .NET,但它不是 ASP.NET。这意味着,我自己而不是使用 ASP.NET 生成 html 代码,并且我正在直接使用 GET 字符串和 POST 流。了解幕后发生的事情是一个很好的练习 :D,尤其是现在,当一切都有一个框架时。
I've been trying to find information about this, but I found several approaches, some of them javascript (thing I want to avoid for the time being) and lots of premade controls. What I want is to do it myself, I don't care if there is a nice, nifty and well-proven ASP.NET control... what I want is understand how it do that with all its implications.
我一直在试图找到有关此的信息,但我发现了几种方法,其中一些是 javascript(我暂时想避免的事情)和许多预制控件。我想要的是自己做,我不在乎是否有一个漂亮的、漂亮的和经过充分验证的 ASP.NET 控件......我想要的是了解它是如何做到这一点的。
Cheers!
干杯!
回答by Jon Hanna
In the HTML you need a form with an input of type="file" and the enctype attribute of the form set to "multipart/form-data" rather than the default of "application/x-www-form-urlencoded".
在 HTML 中,您需要一个 type="file" 输入的表单,并且表单的 enctype 属性设置为“multipart/form-data”,而不是默认的“application/x-www-form-urlencoded”。
Multipart/form-data is defined in RFC 2388, and will behave differently to the application/x-www-form-urlencoded you've been parsing with this experiment so far, though it's quite straight-forward. The RFC should give you all you need to know to replicate how the HttpRequest.Files property works in ASP.NET.
Multipart/form-data 是在RFC 2388 中定义的,它的行为与迄今为止您在这个实验中解析的 application/x-www-form-urlencoded 有所不同,尽管它非常简单。RFC 应该为您提供复制 HttpRequest.Files 属性在 ASP.NET 中的工作方式所需的全部信息。
An extension of this, try sending streams from XMLHttpRequest in a page or HttpWebRequest in a .NET client application, using both POST and PUT (you may have to change IIS settings to allow the PUT through), as this the overlap of working on that along with your experiments here will cover some knowledge that has some real applicability even when you are using all the toolkits. Another extension is to try implementing both sides of both schemes in RFC2617without any help from the framework (sometimes the server side of this is genuinely useful).
对此的扩展,尝试从页面中的 XMLHttpRequest 或 .NET 客户端应用程序中的 HttpWebRequest 发送流,同时使用 POST 和 PUT(您可能必须更改 IIS 设置以允许 PUT 通过),因为这是工作的重叠连同您在此处的实验,将涵盖一些知识,即使您使用所有工具包,这些知识也具有一定的实际适用性。另一个扩展是尝试在RFC2617 中实现两种方案的双方,而无需框架的任何帮助(有时服务器端真的很有用)。
Kudos for experimenting with this, it should bring real experience to back up what you can learn from reading RFC 2616(though that's still absolutely vital for anyone doing web stuff to be intimiately familiar with, as reading will cover some cases your experiments don't touch on, and explain anything that seems strange in your results).
对此进行实验的荣誉,它应该带来真实的经验来支持您可以从阅读RFC 2616中学到的东西(尽管这对于任何从事网络工作的人都非常熟悉,这仍然是绝对重要的,因为阅读将涵盖您的实验没有的某些情况触摸并解释结果中似乎奇怪的任何内容)。
回答by Paddy
回答by Guillaume Lebourgeois
<input type="file" name="somename" size="n">
You put that in a form, and hasta la vista baby !
你把它写成一个表格,并且hasta la vista baby!
回答by Pekka
You can't do a file upload using pure HTML: You will need to handle the uploaded files on server side.
您不能使用纯 HTML 进行文件上传:您需要在服务器端处理上传的文件。
You could parse the uploaded file(s) out of the raw POST data if you want to learn how it works "under the hood" (see herefor an example how to do it in ASP), but you willneed some kind of server side language to do it.
如果您想了解它是如何“在后台”工作的,您可以从原始 POST 数据中解析上传的文件(有关如何在 ASP 中执行此操作的示例,请参见此处),但您将需要某种服务器方言来做。