简体   繁体   English

使用javascript将文件上传到网站

[英]Uploading files to a website with javascript

First of all, I have no experience and very, VERY, basic knowledge in web development. 首先,我没有网络开发方面的经验,并且非常非常基础的知识。 So please bear with me a bit. 所以请多多包涵

I have written a python function to upload files to S3. 我已经编写了一个python函数将文件上传到S3。 Works like a charm for all my non web scripts and applications. 对于我所有的非Web脚本和应用程序来说,它就像一个魅力。 I now need to be able to use this function on a website, via an upload button. 现在,我需要能够通过上传按钮在网站上使用此功能。 I have the browse button and the button (or whatever the correct terminology for that is): 我有“浏览”按钮和“按钮”(或其他正确的术语):

    <div id="uploader">
        <input type="file" id="uploadme" />
        <input type="button" id="clickme" value="Upload!" />
    </div>

Now, I was planning to use JavaScript to call the Python function and upload the file. 现在,我正计划使用JavaScript调用Python函数并上传文件。 My questions are: 我的问题是:

  1. How do I do this in JavaScript with the 2 buttons I have above? 如何使用上面的2个按钮在JavaScript中执行此操作?
  2. How do I check which user will the python function run in the server? 如何检查python函数将在服务器中运行哪个用户? Something among the lines of running whoami . whoami跑了的东西。
  3. Is there a better and/or easier way of doing this? 是否有更好和/或更容易的方法?

On the javascript side, the way you get the file is by accessing the file object. 在javascript方面,获取文件的方式是通过访问文件对象。 There are multiple ways to do this. 有多种方法可以做到这一点。 MDN does a great job explaining it here: https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications MDN在这里进行了出色的解释: https : //developer.mozilla.org/en-US/docs/Using_files_from_web_applications

On question 2, you can use the os library in python. 关于问题2,您可以在python中使用os库。 I am assuming you are running your python script in linux or OSX. 我假设您正在linux或OSX中运行python脚本。

import os os.getenv('USER') #this should return the username from the environment variable

Hope this helps. 希望这可以帮助。

This is not possible. 这是不可能的。 Input elements <input type="file"> are just calling the internal browser interface to the OS. 输入元素<input type="file">只是调用操作系统的内部浏览器界面。 This will then open the OS file picker dialog. 然后将打开OS文件选择器对话框。

You can place form in a frame and upload it. 您可以将表单放在框架中并上传。 Or html file object: developer.mozilla.org/en/docs/Using_files_from_web_applications 或html文件对象:developer.mozilla.org/en/docs/Using_files_from_web_applications

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

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