从html表单获取文件位置(Getting file location from an html form)

编程入门 行业动态 更新时间:2024-10-23 03:15:10
从html表单获取文件位置(Getting file location from an html form)

编辑:根据Martijn Pieters的建议,为我的代码添加了解决方案。

我正在编写一个应该具有图片上传功能的Web应用程序。 实际上它是通过他们的api直接上传到imgur.com。 到目前为止,我可以通过运行我的python脚本通过终端上传图像到imgur。 但我希望用户能够通过html表单选择图像。 所有我真正需要的(我认为)是文件的路径,然后我有一个脚本将图像转换为base64。 在我写这篇文章时,我意识到脚本可能只在本地运行时才有效,而不是从浏览器运行。 我一直在谷歌上搜索两天。 我得到了很多关于cgi.FieldStorage()的结果,但似乎无法让我工作,因为我得到了一个KeyError。

这是我表单的文件输入部分:

<div class="container"> <form role="form" class="form-horizontal form-inline" method="post" enctype="multipart/form-data"> <div class="col-md-12"> <div class="form-group"> <label for="image" class="control-label">Product image</label> <input type="file" id="image" name="imageurl" value=""/> </div> </div>

然后我需要能够处理我的python脚本中的输入:

class MainPage(BlogHandler): def get(self): self.render("front.html") def post(self): image = self.request.POST.get("imageurl") logging.info(getUrl(image))

这是将图像发送到imgur api的代码:

def getUrl(image): API_URL = "https://api.imgur.com/3/upload.json" image_read = image.file.read() b64 = base64.b64encode(image_read) data = { 'image': b64, 'type': base64, 'title': 'testupload' } response = requests.post(API_URL, data, headers={'Authorization': 'Client-ID my-cient-id'}) url = response.json()['data']['link'] return url

我在Google App Engine中开发并使用Jinja2作为模板引擎。

我是初学程序员,所以我希望得到一些关于我的问题的指示。 我觉得我一直在寻找答案。 我已经读过,出于安全原因,浏览器对计算机的文件系统一无所知,但是存在可以从计算机中选择要上传的文件的网站,所以我认为必须能够实现。 :)

编辑:根据Martijn Pieters的建议,为我的代码添加了解决方案。

EDIT: Added the solution to my code, as suggested by Martijn Pieters.

I'm writing a web application that should have an image upload feature. Actually it is uploading directly to imgur.com through their api. So far I can upload an image to imgur through the terminal by running my python script. But I want the user to be able to select an image through an html form. All I actually need (I think) is the path for the file, then I have a script that converts the image to base64. As I write this I realize that the script might only work when it's run locally, and not from a browser. I have been googling for two days. I get a lot of results about cgi.FieldStorage() but can't seem to get that to work as I get a KeyError.

This is the file input part of my form:

<div class="container"> <form role="form" class="form-horizontal form-inline" method="post" enctype="multipart/form-data"> <div class="col-md-12"> <div class="form-group"> <label for="image" class="control-label">Product image</label> <input type="file" id="image" name="imageurl" value=""/> </div> </div>

And then I need to be able to handle the input in my python script:

class MainPage(BlogHandler): def get(self): self.render("front.html") def post(self): image = self.request.POST.get("imageurl") logging.info(getUrl(image))

This is the code for sending the image to the imgur api:

def getUrl(image): API_URL = "https://api.imgur.com/3/upload.json" image_read = image.file.read() b64 = base64.b64encode(image_read) data = { 'image': b64, 'type': base64, 'title': 'testupload' } response = requests.post(API_URL, data, headers={'Authorization': 'Client-ID my-cient-id'}) url = response.json()['data']['link'] return url

I developing in Google App Engine and using Jinja2 as the templating engine.

I'm a beginner programmer so I hope to get some pointers on my problem. I feel like I'm stuck on searching for the answer. I've read that the browser does not know anything about the file system of the computer for security reasons, but websites exist where you can choose a file from your computer to upload, so I figure it must be possible to achieve. :)

EDIT: Added the solution to my code, as suggested by Martijn Pieters.

最满意答案

上传图像需要您将表单编码更改为multipart/form-data :

<form encoding="multipart/form-data" role="form" class="form-horizontal form-inline" method="post">

请参阅application / x-www-form-urlencoded或multipart / form-data?

因为这是一个POST请求,所以你会在request.POST找到这些字段:

def post(self): imageurl = self.request.POST.get("imageurl") logging.info(imageurl)

虽然request.get()将搜索查询参数和POST表单数据。

表单上载作为cgi.FieldStorage()对象返回; 它有一个file属性,可以为您提供类似文件的界面。 你可以把数据读入内存:

image = self.request.POST.get("imageurl") image_read = image.file.read() b64 = base64.b64encode(image_read)

Uploading an image requires that you change the form encoding to multipart/form-data:

<form encoding="multipart/form-data" role="form" class="form-horizontal form-inline" method="post">

See application/x-www-form-urlencoded or multipart/form-data?

Because this is a POST request, you'll find the fields in request.POST:

def post(self): imageurl = self.request.POST.get("imageurl") logging.info(imageurl)

although request.get() will search both query parameters and POST form data.

A form upload is returned as a cgi.FieldStorage() object; this has a file attribute, which gives you a file-like interface. You could just read the data into memory:

image = self.request.POST.get("imageurl") image_read = image.file.read() b64 = base64.b64encode(image_read)

更多推荐

本文发布于:2023-07-24 01:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240127.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   位置   文件   html   location

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!