jquploader + bottle.py-不能让它工作:(

时间:2012-09-24 09:04:50

标签: python file-upload web

我有一个简单的bottle.py应用程序,让我上传文件。既然这些文件可能很大,我想要一个进度条。我发现jqUploader看起来就像我想要的那样,但现在我无法让它工作。 当我拥有自己的表格时,我的上传工作非常完美,但现在我不知道如何正确访问数据了。

吡啶:

@route('/upload')
def upload():
  return static_file('upload_form.html', root='html')

@post('/load_from_file')
def load_from_file():
  print("Uploading...")
  name = request.forms.name
  data = request.files.myFile3
  print(data)
  filename = data.filename
  with open(os.path.join("C:\\", filename), "wb") as file_object:
      bytes = 0
      while True:
          datachunk = data.file.read(1024)
          if not datachunk:
              break            
          file_object.write(datachunk)
          bytes  += len(datachunk)
  return "Hello %s! You uploaded <b>%s</b>." % (name, filename)

HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>jqUploader demo</title>
    <link rel="stylesheet" type="text/css" media="screen" href="static/style.css"/>
    <script type="text/javascript" src="static/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="static/jquery.flash.js"></script>
    <script type="text/javascript" src="static/jquery.jqUploader.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $('#example1').jqUploader({
                debug:0
                ,background:'FFFFDF'
                ,barColor:'FFDD00'
                ,allowedExt:'*.txt; *.xml;'
                ,allowedExtDescr: 'what you want'
                ,validFileMessage: 'Thanks, now hit Upload!'
                ,endMessage: 'and don\'t you come back ;)'
                ,hideSubmit: false
            });
            $("#example2").jqUploader({
                afterScript:    "redirected.php",
                background: "FFFFDF",
                barColor:   "64A9F6",
                allowedExt:     "*.avi; *.jpg; *.jpeg; *.png",
                allowedExtDescr: "Images and movies (*.avi; *.jpg; *.jpeg; *.png)"
            });

            $("#example3").jqUploader({
            background: "FFFFDF"
            ,barColor:  "FF00FF"
            ,allowedExt:'*.txt; *.xml;'
            ,allowedExtDescr: 'what you want'
            ,validFileMessage: 'Thanks, now hit Upload!'
            });
        });
    </script>
</head>
<body>
        <h3>The form</h3>
        <form enctype="multipart/form-data" action="/load_from_file" method="POST" class="a_form">
            <fieldset>
                <legend>Upload your file</legend>
                <ol>
                    <li id="example3">
                        <label for="example3_field">Choose a file to upload:</label>
                        <input name="myFile3" id="example3_field"  type="file" />
                    </li>
                </ol>
            </fieldset>
            <input type="submit" name="submit" value="Upload File" />
        </form>


    </div>
</body>

ERROR:

  

AttributeError:'str'对象没有属性'filename'

非常感谢帮助!

1 个答案:

答案 0 :(得分:0)

这是工作代码(没有更改html或js中的任何内容):

@route('/upload')
def upload():
    #Upload-Form
    return static_file('upload_form.html', root='html')

@post('/load_from_file')
def load_from_file():
    #Actual Uploading
    if request.POST.Upload == 'Submit Query':
        try:
            data = request.POST.Filedata.file
            filename = request.POST.Filename
            filepath = os.path.join(settings.FOLDERS["TEMP_WORKING"], filename)
            with open(filepath, "wb") as file_object:
                while True:
                    datachunk = data.file.read(1024)
                    if not datachunk:
                        break            
                    file_object.write(datachunk)
            process_files_upload(filepath)            
        except:
            print(traceback.format_exc())
        #Check if only uploaded or already finished
        return "Upload Finished"
    else:
        redirect('/upload')