【问题标题】:Image Uploading Python图片上传 Python
【发布时间】:2014-04-21 10:40:57
【问题描述】:

我想使用 python 浏览图像并将图像上传到我的应用程序中的文件夹

当我点击提交按钮时,它显示http://www.domain.com/store_mp3_view & 图片未上传

html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="/store_mp3_view" method="post" accept-charset="utf-8"
      enctype="multipart/form-data">

    <label for="mp3">Mp3</label>
    <input id="mp3" name="mp3" type="file" value="" />

    <input type="submit" value="submit" />
</form>
</body>
</html>

python 文件代码 导入操作系统 导入 uuid 从pyramid.response导入响应

def store_mp3_view(请求): # filename 包含字符串格式的文件名。 # # 警告:这个例子没有处理 IE 发送一个 # 绝对文件 path 作为文件名。这个例子很幼稚;它 # 信任用户输入。

filename = request.POST['mp3'].filename

# ``input_file`` contains the actual file data which needs to be
# stored somewhere.

input_file = request.POST['mp3'].file

# Note that we are generating our own filename instead of trusting
# the incoming filename since that might result in insecure paths.
# Please note that in a real application you would not use /tmp,
# and if you write to an untrusted location you will need to do
# some extra work to prevent symlink attacks.

file_path = os.path.join(/files, '%s.mp3' % uuid.uuid4())

# We first write to a temporary file to prevent incomplete files from
# being used.

temp_file_path = file_path + '~'
output_file = open(temp_file_path, 'wb')

# Finally write the data to a temporary file
input_file.seek(0)
while True:
    data = input_file.read(2<<16)
    if not data:
        break
    output_file.write(data)

# If your data is really critical you may want to force it to disk first
# using output_file.flush(); os.fsync(output_file.fileno())

output_file.close()

# Now that we know the file has been fully saved to disk move it into place.

os.rename(temp_file_path, file_path)

return Response('OK')
return Response('OK')

【问题讨论】:

  • 这甚至不是正确的 python 代码。在你的函数中加入一些调试代码,看看会发生什么。
  • 这是 Django 还是类似的东西?你应该相应地标记它。

标签: python html


【解决方案1】:

您可以在 models.py 中使用模型文件字段

class Document(models.Model):
    docfile = models.FileField(upload_to='documents/', max_length=5234,blank=True, null=True,)

对应的forms.py

class DocumentForm(forms.Form):
    docfile = forms.FileField(label='', show_hidden_initial='none',required=True,)

内部views.py

if request.FILES.has_key('your_fileName'):
            newdoc = Document(docfile = request.FILES['your_fileName'])
            newdoc.save()

我用上面的代码搞定了,希望对你有帮助

【讨论】:

  • 是否必须包含模型、表单、视图 python 文件? & your_fileName 表示哪个文件?
  • 是的 user3322102 你必须包含模型、表单和视图。 docfile 我们在模型和表单中使用的
【解决方案2】:

使用此代码,您可以上传多个文件

def insert_file(self):
    for i in request.FILES.getlist('mp3'):
        fileName = i.name
        out_file = open(fileName,'w')
        out_file.write(i.read())
    return HttpResponse('Inserted Successfully')

【讨论】:

  • @user3322102,检查我上面的答案,这可能对你有用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-12
  • 1970-01-01
  • 2012-05-17
  • 2013-09-14
  • 1970-01-01
相关资源
最近更新 更多