We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
html端 这里只使用file type的input,不使用form来上传
<input type="file" id="upload_file_name" name="upload" /> <input type="button" value="上传图片" onclick="upload_image()"/> <span id="upload_status"></span><br/>
js端 formData.append可以添加你需要的其他数据上去,这里我添加了一个"action": "upload_image"上去
function flush_result(obj) { if(obj["action"] == "upload_image"){ if(obj["error"] != "ok"){ $("#upload_status").text("上传失败"); } else{ $("#upload_status").text('上传成功'); } return; } } function upload_image(){ var formData = new FormData(); formData.append('action', 'upload_image') formData.append('file', $('#upload_file_name')[0].files[0]); $.ajax({ url: '/cgi-bin/feeling.py', type: 'POST', cache: false, data: formData, processData: false, contentType: false }).done(function(res){ flush_result(res); }).fail(function(res){ flush_result(res); }); }
python端 保存文件到/tmp目录下
#!/usr/bin/python3 import os,sys,requests,cgi,json import codecs def upload_image_action(form): response = {} response["action"] = form.getvalue('action') fileitem = form['file'] try: fn = os.path.basename(fileitem.filename) open('/tmp/' + fn, 'wb').write(fileitem.file.read()) response["error"] = "ok" except: response["error"] = "error" print("Content-type: application/json\r\n\r\n") print(json.dumps(response)) form = cgi.FieldStorage() action = form.getvalue('action') if action == 'upload_image': upload_image_action(form)
ok!
The text was updated successfully, but these errors were encountered:
No branches or pull requests
html传输文件-python后台接收文件
html端
这里只使用file type的input,不使用form来上传
js端
formData.append可以添加你需要的其他数据上去,这里我添加了一个"action": "upload_image"上去
python端
保存文件到/tmp目录下
ok!
The text was updated successfully, but these errors were encountered: