0

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

py:

@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>

错误:

AttributeError:“str”对象没有属性“文件名”

帮助将不胜感激!

4

1 回答 1

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')
于 2012-09-24T18:19:07.600 回答