上传具有附加信息的文件处理程序

时间:2015-03-23 14:29:43

标签: javascript python ajax google-app-engine webapp2

我正在使用具有客户端数据对象的WebApp2创建GAE应用程序。每个客户端都有一个唯一的ID,我希望允许用户上传与该特定客户端对象关联的一个或多个文件。我知道这可以使用blobstore,GCS,但我正在试验Titan Files,它能够使用blobstore后端,同时允许我为我的用户创建一个准文件目录系统。我是GAE编程的新手,所以非常感谢任何帮助!

我正在使用FormData object以便将其他有用的信息附加到我的文件中(文件名,以及带有“文件夹”和“我要写入文件的子文件夹”的目录路径) 。从firebug控制台,我的帖子似乎正在正常触发,因为我看到有很多奇怪字符(文件)的帖子以及我附加到文件的其他数据(我希望它写入的文件名和路径如上所述) 。也就是说,我收到以下错误:

Traceback (most recent call last):
File "C:\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "C:\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "C:\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__
return handler.dispatch()
File "C:\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch
return method(*args, **kwargs)

File "C:\Users\Owner\Documents\vein\vann-vein\main.py", line 121, in post
myfile = self.request.post(self)
File "C:\lib\webob-1.1.1\webob\request.py", line 1238, in __getattr__
raise AttributeError(attr)
AttributeError: post
INFO     2015-03-21 13:55:19,667 module.py:666] default: "POST /uploadfile     HTTP/1.1" 500 1469

我相当确定问题出在我的UploadHandler中,但是会为完整的上下文提供其他MVC元素......我不清楚我是否正确使用GET / POST。

我的HTML表单:

    <div class="form-group">
        <label class="control-label col-sm-4" for="clientNumber">Client Number*:</label>
            <div class="col-sm-8">
                <input type="text" class="form-control" id="clientNumber" placeholder="Client 1111111" disabled>
            </div>
    </div>

    <div class="form-group">   
        <label class="control-label col-sm-4" for="clientDocs">Client Docs:</label>                     
            <div class="col-sm-8">
                <input type="file" type="file" name="file" id="file"><br>
                <button type="button" class="btn btn-info" id="clientDocsButton" onclick="addFile()" disabled>Add File</button><br>
            </div>
    </div>

我的Javascript被调用:

function addFile()
{
console.log("Pre-Instantiation.");
var filepath = window.location.pathname;
var subfolder = document.getElementById("clientNumber").value
var myfile = document.getElementById("file");
var filename = document.getElementById("file").value;

console.log("Post-Instantiation.");
var formData = new FormData();
formData.append('thefile', myfile.files[0]);
formData.append('filepath', filepath);
formData.append('subfolder', subfolder);
formData.append('filename', filename);
console.log("Pre-AJAX.");
console.log(typeof formData['thefile']);

$.ajax({
    url: '/uploadfile',  //Server script to process data
    type: 'POST',
    xhr: function() {  // Custom XMLHttpRequest
        var myXhr = $.ajaxSettings.xhr();
        if(myXhr.upload){ // Check if upload property exists
            myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload
        }
        return myXhr;
    },
    //Ajax events
    success: function(response) {
        bootbox.alert(response);
    },
    error : function(xhr,errmsg,err) {
        bootbox.alert(xhr.status);
    },
    data: formData,
    //Options to tell jQuery not to process data or worry about content-type.
    cache: false,
    contentType: false,
    processData: false
})};

我的主要 - UploadHandler:

class UploadHandler(webapp2.RequestHandler):
def post(self):
    myfile = self.request.post(self)
    myfile = formData['thefile']
    filepath = myfile['filepath']
    subfolder = myfile['subfolder']
    filename = myfile['filename']

    files.File('/' + filepath + '/' + subfolder + '/' + filename).write(myfile)
    self.response.write("Save successful")

注意:上面的files.File()。write()方法是Titan Files库的一部分,它被导入到我的Main.py中

1 个答案:

答案 0 :(得分:0)

您可以通过get方法从请求对象获取所有提交的数据,无需创建任何其他对象:

class UploadHandler(webapp2.RequestHandler):
    def post(self):
        myfile = self.request.get('thefile')
        filepath = self.request.get('filepath')
        subfolder = self.request.get('subfolder')
        filename = self.request.get('filename')

        files.File('/' + filepath + '/' + subfolder + '/' + filename).write(myfile)
        self.response.write("Save successful")