2012-09-24 58 views
0

我有一個簡單的bottle.py應用程序讓我上傳文件,現在因爲這些文件可能很大,我想要一個進度條。 jqUploader看起來像是我想要的,但現在我很難讓它工作。 我的上傳工作完美,當我有我自己的形式,但現在我不知道如何正確訪問數據了。潘岳:jquploader + bottle.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' object has no attribute 'filename'

幫助,將不勝感激!

回答

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')