简体   繁体   English

用于文件上载的HTML表单 - 由JavaScript生成

[英]HTML Form for File Upload - generate by JavaScript

I've been trying to create and submit a form with JavaScript for file upload to my web server. 我一直在尝试创建并提交带有JavaScript的表单,以便将文件上传到我的Web服务器。 I've read that the input type="file" cannot be autopopulated by JavaScript because of security reasons. 我已经读过,由于安全原因, input type="file"不能由JavaScript自动填充。 That being the case, how can I send a POST command to upload my file? 既然如此,我如何发送POST命令来上传我的文件?

I realise the code snippet below won't work becase of this fact but how can I do it? 我意识到下面的代码片段不会因为这个事实而起作用,但我怎么能这样做呢?

What I'm hoping to do in the end is call this function for a number of file that the user will drag and drop so they'll be in an array. 我最后希望做的是将这个函数调用为一个用户将拖放的文件,这样它们就会在一个数组中。 I don't have PHP on my webserver (it will only hand simple HTTP commands). 我的网络服务器上没有PHP(它只会处理简单的HTTP命令)。

 function uploadFile( fileName )
   {
     var form     = document.createElement("form");
     var element1 = document.createElement("file");

     form.method  = "post";
     form.action  = "upload.html";
     form.enctype = "multipart/form-data";
     form.charset = "utf-8";

     element1.value = "/Temp/TestFile.txt";
     element1.name = "upload1";
     form.appendChild( element1 );

     document.body.appendChild( form );

     form.submit();
   }

After reading the first link provided by Tom, this is what I came up with: 在阅读了Tom提供的第一个链接后,这就是我想出的:

   function uploadFile( fileName )
   {
     var form     = document.createElement("form");
     var element1 = document.createElement("file");

     form.method  = "post";
     form.action  = "upload.html";
     form.enctype = "multipart/form-data";
     form.charset = "utf-8";

     element1.value = "/JScript/TestText.txt"
     element1.id    = "fileselect";
     element1.name  = "fileselect[]";
     element1.multiple = "multiple";
     form.appendChild( element1 );

     document.body.appendChild( form );

     form.submit();
   }

..but I don't understand the use of fileselect[].. That I would understand to be an array but how do I populate an array and then assign it back to the HTML element? ..但我不明白fileselect []的使用..我会理解为一个数组,但我如何填充数组然后将其分配回HTML元素? Say for example I only have one file to post back which is /JScript/TestText.txt .. how do I assign fileselect[0]="/JScript/TestText.txt"; 比方说,我只有一个文件要回发,这是/JScript/TestText.txt ..如何分配fileselect [0] =“/ JScript / TestText.txt”; and then feed it back into 'element' for submission? 然后将其反馈回'element'进行提交?

Correct you can't populate the value of a file input. 更正您无法填充文件输入的值。 If you could, you could access any file on a user's computer, which would be a huge security issue. 如果可以,您可以访问用户计算机上的任何文件,这将是一个巨大的安全问题。

Also, you won't be able to upload the file to the server at all without server-side scripting of some kind to validate the file and store it to the correct location. 此外,如果没有某种服务器端脚本来验证文件并将其存储到正确的位置,您将无法将文件上传到服务器。 If it was possible to do that with just apache, a visitor to your site could upload whatever they wanted to your server, including viruses. 如果只使用apache就可以做到这一点,那么您网站的访问者可以将他们想要的任何内容上传到您的服务器,包括病毒。

If you do manage to get server side uploads working (I would test with a static html form with a standard file input field first), then in order for you to accept a file via drag and drop and push that file to the server you'll need the html5 file api. 如果您确实设法使服务器端上传工作(我将首先使用带有标准文件输入字段的静态html表单进行测试),那么为了让您通过拖放接受文件并将该文件推送到服务器''我需要html5文件api。 Plenty of tutorials like this one . 很多像这样的教程。

If you want something robust that will work easily cross platform, have a look at something like PlUpload which achieves everything you want, from the client side at least. 如果你想要一些可以轻松跨平台工作的强大功能,那么看看像PlUpload这样的东西,它至少可以从客户端实现你想要的一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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