简体   繁体   English

需要将上传的文件上传到我的本地PC

[英]Need to get the uploaded file to my local PC

I have created a test form which will ask users to enter a name and upload the image file: 我创建了一个测试表单,该表单将要求用户输入名称并上传图像文件:

<html lang="en">
<head>
    <title>Testing image upload</title>
</head>
<body>
    <form action="/services/upload" method="POST" enctype="multipart/form-data">
    File Description: <input name='fdesc' type='text'><br>
    File name: <input type="file" name="fname"><br>
    <div><input type="submit"></div>
    </form>
</body>
</html>

i need to get the file uploaded by the user and store it on my local PC. 我需要获取用户上传的文件并将其存储在本地PC上。 can this be done in python ? 可以在python中完成吗? please let me know. 请告诉我。

mod_python includes the FieldStorage class which allows you access to uploaded form data. mod_python包含FieldStorage该类使您可以访问上载的表单数据。 In order to use it, you'd put something like the following in your Python script: 为了使用它,您需要在Python脚本中添加以下内容:

req.form = FieldStorage(req)
description = req.form['fdesc']

Since fdesc is a text input, description will be a string (more precisely, a StringField , which you can treat as a string). 由于fdesc是文本输入,因此description将是一个字符串(更确切地说是一个StringField ,您可以将其视为字符串)。

file_field = req.form['fname']

Since fname is a file input, file_field will not be a string (or StringField ), but rather a Field object which allows you access to the file data. 由于fname是文件输入,因此file_field将不是字符串(或StringField ),而是允许您访问文件数据的Field对象。 The attribute file_field.file is a file-like object which you can use to read the file's contents, for example like so: 属性file_field.file是一个类似文件的对象,您可以使用它来读取文件的内容,例如:

for line in file_field.file:
    # process the line

You could use this to copy the file's data somewhere of your choosing, for example. 例如,您可以使用它来将文件的数据复制到您选择的某个位置。

file_field.filename is the name of the file as provided by the client. file_field.filename是客户端提供的文件名。 Other useful attributes are listed in the documentation I linked to. 我链接到的文档中列出了其他有用的属性。

也许最小的http cgi上传食谱 ,它的评论对您有帮助。

Hey David i got it working, i did it this way: 嗨,大卫,我成功了,我这样做是这样的:

filename = request.FILES['fname']
destination = open('%s/%s'%(/tmp/,fileName), 'wb+')
for chunk in filename.chunks():
            destination.write(chunk)
destination.close()

file = open('%s/%s'%(/tmp/,fileName),"rb").read()

Thanks for the help guys. 感谢您的帮助。

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

相关问题 获取Django中上传文件的本地路径 - get local path of an uploaded file in django 我是否需要在本地PC上安装MySQL才能使用MySQLdb for Python远程连接MySQL服务器? - Do I need MySQL installed on my local PC to use MySQLdb for Python to connect MySQL server remotely? 将上传的二进制文件保存到本地文件 - Saving uploaded binary to local file 无法从我的本地 PC 和 AWS EC2 实例的 Twitter Web 获得相同的请求结果 - Unable to get the same requests results from Twitter Web from my local PC and AWS EC2 instance 我如何每分钟自动将一个新的 csv 文件从我的本地电脑导入到谷歌表格? - How do i automatically import a new csv file from my local pc to google sheets every minute? AWS Lambda Python:我可以在调用 lambda 时从本地 PC 加载文件吗? - AWS Lambda Python: Can I load a file from local PC when calling my lambda? Google Colab:本地 PC 上没有此类文件或目录 - Google Colab: No such file or directory on local PC 什么是“本地目录”? 为什么它与我电脑上的“本地”不同? - What is "the local directory"? Why is it different from "Local" on my PC? 获取上传的文件名 (Python) - Get uploaded file name (Python) 如何在 Flask 中获取上传的文件? - How to get uploaded file in Flask?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM