简体   繁体   English

Javascript:保存上传的文件

[英]Javascript: Save uploaded file

I would like to save uploaded file using javascript, in my linux server.我想在我的 linux 服务器中使用 javascript 保存上传的文件。 The code I wrote is:我写的代码是:

if (uploadInput.files.length == 0) {
            console.log("No file is uploaded. ");
    } else {
            console.log("File uploaded.");
            var file = uploadInput.files[0];                
            }

Now I would like to save that file as "files/upload.csv".现在我想将该文件保存为“files/upload.csv”。 Can anyone please advise, how can I do it?任何人都可以请指教,我该怎么做?

Thanks in advance提前致谢

What I'm going to do is walk you through the logic, instead of providing code.我要做的是引导您完成逻辑,而不是提供代码。 There is just not enough information here on what you want to do to provide actual code and the sample you provided is a very small part of what the actual solution would need to include.这里没有足够的信息来说明您想要做什么来提供实际代码,您提供的示例只是实际解决方案需要包含的内容的很小一部分。

I'm assuming the code you wrote above is meant to run on a website visitor's browser (client-side).我假设您上面编写的代码旨在在网站访问者的浏览器(客户端)上运行。 Client-side code can't save to a server.客户端代码无法保存到服务器。 What it can do, is send the file contents to the server.它可以做的是将文件内容发送到服务器。 But then you'd need something on the server side to process that file contents and actually save it to the server-side files directory.但是随后您需要在服务器端进行一些操作来处理该文件内容并将其实际保存到服务器端文件目录中。

One method to send the file contents from the client to the server is to use AJAX - you can do this with native javascript, but I would recommend looking into a library such as Jquery, which makes it a lot easier.将文件内容从客户端发送到服务器的一种方法是使用 AJAX - 您可以使用本机 javascript 执行此操作,但我建议您查看诸如 Jquery 之类的库,这使它变得容易得多。 See http://api.jquery.com/jquery.ajax/ This AJAX code will need a communication point on the server to send the file contents to.参见http://api.jquery.com/jquery.ajax/这个 AJAX 代码需要服务器上的一个通信点来发送文件内容。 From your profile it seems you're familiar with PHP.从您的个人资料来看,您似乎对 PHP 很熟悉。 You could make a php file on the server (say receivefilecontents.php) that takes in input from that client-side AJAX call, and then saves it to a server directory - you could also do this in Python, Java or a number of other languages.您可以在服务器上创建一个 php 文件(比如 receivefilecontents.php),该文件接收来自客户端 AJAX 调用的输入,然后将其保存到服务器目录 - 您也可以在 Python、Java 或其他一些语言。

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

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