带有附加信息的文件的上载处理程序

Upload Handler for File with Appended Information

本文关键字:文件 上载 处理 程序 信息      更新时间:2023-09-26

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

我使用FormData对象是为了将其他有用的信息附加到我的文件中(文件名,以及我想将文件写入的带有"文件夹"answers"子文件夹"的目录路径)。从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
})};

我的主上传处理程序:

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

注意:文件。上面的File().write()方法是Titan Files库的一部分,该库正在导入到我的Main.py 中

您可以通过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")