简体   繁体   English

将文件转换为URL中的可下载文件-javascript

[英]Convert a file into a downloadable file in url - javascript

Is it possible to upload a file who is locally stored in our computer, in a server, and have a final url to download it ? 是否可以上传本地存储在我们计算机中,服务器中的文件,并具有下载该文件的最终URL?

Like this exemple ( this is an excel file ) : 像这个例子( 这是一个excel文件 ):

http://mapa.aji-france.com/mapa/file/marche/18143/DPGF%20VIERGE%20URUGUAY%20LABORATOIRES%20H174-H176-BUANDERIE.xls http://mapa.aji-france.com/mapa/file/marche/18143/DPGF%20VIERGE%20URUGUAY%20LABORATOIRES%20H174-H176-BUANDERIE.xls

I need to do this because i'm using an API (who do a excel preview with a .xls file on a web page), and we need to have the url of the file.. 我需要执行此操作,因为我使用的是API(使用网页上的.xls文件执行excel预览的API),并且我们需要该文件的网址。

You need some server-side mechanism to handle the uploaded file (eg a PHP script or a servlet or whatnot). 您需要某种服务器端机制来处理上载的文件(例如,PHP脚本或servlet或诸如此类)。 You cannot directly access the server file system with Javascript, as Javascript runs locally in the user's browser. 您无法直接使用Javascript访问服务器文件系统,因为Javascript在用户浏览器中本地运行。 So just create a simple server side script that stores the uploaded file and returns the server side file location. 因此,只需创建一个简单的服务器端脚本即可存储上载的文件并返回服务器端文件的位置。

You need to do more research around this until you can attempt it. 您需要对此进行更多研究,直到可以尝试为止。 Heres a quick overview using PHP, I know you specified Javascript but PHP is very good at this. 这里是使用PHP的快速概述,我知道您指定了Javascript,但是PHP在这方面非常擅长。

You would have to create two files, a html file where you can upload the file from graphically, and a PHP file to take that file and put it on the server. 您将必须创建两个文件,一个html文件(可从其中以图形方式上传该文件)和一个PHP文件以将该文件放入服务器中。

The HTML: HTML:

<form hidden action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="fileToUpload" id="fileToUpload" class="ui button">
    <input type="submit" value="upload" name="submit" class="ui primary button">
</form>

The php upload.php : 的php upload.php

<?php
   $target_file = "where/to/upload/remotely" . basename($_FILES["fileToUpload"]["name"]);
   move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file) or die ("failed");
?>

You should store both these files in your public html directory, usually /var/www/html on unix. 您应该将这两个文件都存储在公共html目录中,通常是unix上的/var/www/html The PHP file needs permissions that allow it to be executed. PHP文件需要允许其执行的权限。 Theres a bit of configuration to this which would be hard to explain online, this is just showing you a very simple abstraction of how it would work. 对此有一些配置,很难在网上进行解释,这只是向您显示了其工作方式的非常简单的抽象。

On submission of the html form, so on submit button clicked, upload.php is executed (as its the action of the form), this gets the filename and moves it onto the specified directory on the server. 提交html表单后,单击“提交”按钮,执行upload.php (作为表单的动作),这将获取文件名并将其移至服务器上的指定目录。

Then assuming the upload was successful the remote file name would be the same as the files original name. 然后,假设上传成功,则远程文件名将与文件原始名称相同。 So say I uploaded hello.txt to thisserver.net , the remote path would be thisserver.net/hello.txt 所以说我将hello.txt上传到thisserver.net ,远程路径将是thisserver.net/hello.txt

This code assumes you have a webserver installed, such as apache2 . 此代码假定您已安装Web服务器,例如apache2 It also assumes you have php configured to work with said webserver. 它还假设您已将php配置为可与所述网络服务器一起使用。

Read this for more information. 阅读以获得更多信息。

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

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