【发布时间】:2015-05-26 14:19:23
【问题描述】:
我正在使用具有客户端数据对象的 WebApp2 创建 GAE 应用程序。每个客户端都有一个唯一的 ID,我希望允许用户上传与该特定客户端对象关联的一个或多个文件。我知道使用 blobstore、GCS 可以做到这一点,但我正在尝试使用 Titan Files,它能够使用 blobstore 后端,同时允许我为我的用户创建一个准文件目录系统。我对 GAE 编程相当陌生,因此将不胜感激任何帮助!
我正在使用FormData object 将其他有用的信息附加到我的文件(文件名,以及我想要写入文件的“文件夹”和“子文件夹”的目录路径)。从萤火虫控制台,我的帖子似乎正在正确触发,因为我看到帖子中有很多奇怪的字符(文件)和我附加到文件的其他数据(我希望它写入的文件名和路径,如上所述) .也就是说,我收到以下错误:
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 中
【问题讨论】:
标签: javascript python ajax google-app-engine webapp2