谷歌应用程序引擎如何报告文件上传的进度

发布于 2024-08-06 12:25:42 字数 1557 浏览 1 评论 0原文

我一直在尝试将 ajax 风格的文件上传组件(如 dojox 文件上传组件)与 google 应用引擎一起使用,并且有一个进度条。

javascript 方面的事情很好,但我想知道如何在服务器端组织事情?

是否可以在谷歌应用程序引擎中上传数据时访问数据,也许使用 BaseHTTPServer ?然后我可以从客户端进行轮询。

即使不可能取得进展,至少能够终止一个大文件并让用户知道它太大而不需要用户上传 1MB(谷歌应用程序引擎限制)然后然后就很棒了发现它太大了。

还有一些类似的问题:类似的堆栈溢出问题1类似的堆栈溢出问题 2

以下谷歌应用程序引擎Python代码只是简单文件上传的基本后处理程序(工作正常),但我正在寻找在较低级别运行的东西。当帖子处理程序收到它时,文件已全部上传。

在 LAMP 堆栈上,您可以轮询服务器端脚本来监视临时文件大小的增长。当文件从临时文件夹中移出时,您就知道它已完成。通过使用这种方法,您可以在不使用 flash 的情况下查看多个文件的进度,我不知道如何使用 google app engine 做同样的事情。

class fileupload(webapp.RequestHandler):
"""
"""
def post(self):
    if "image" in self.request.arguments():
        content = "<html><body>Error</body></html>"      
        form = self.request.params['image']
        logging.info(form)
        try:
            filename = form.filename
            siz = len(form.value)

            ###--- Show data
            logging.info("Filename: %s" % form.filename)
            logging.info("Size: %s" % siz)

            content = "<html><body>File has been uploaded Name: %s Size: %s</body></html>" % (filename, siz)
            logging.info("DONE")
        except:     
            logging.info("Error: bad form or something like that")

        logging.info("writing out response")
        self.response.out.write(content)

I have been trying to use an ajax-style file upload component (like the dojox file upload component) with google app engine, and have a progress bar.

The javascript side of things is fine, but i am wondering how to organise things on the server side?

Is is possible to access the data as it is being uploaded within google app engine, something with BaseHTTPServer perhaps? I could then poll this from the client.

Even if it wasn't possible to do the progress, at least it would be great to be able to terminate a large file and let the user know it was to big without making the user upload 1MB (the google app engine limit) and then find out it was to big.

There are some similar questions: Similar stack overflow question 1, Similar stack overflow question 2.

The following google app engine python code is just the basic post handler for a simple file upload (which works fine) but im looking for something that operates at a bit of a lower level. By the time the post handler gets it, the file has all been uploaded.

On a LAMP stack you can poll a server side script that watches a temporary file size grow. When the file is moved from the temporary folder you know its completed. By using this method you can see the progress of multiple files without the use of flash, I'm not sure how to do the same thing with google app engine.

class fileupload(webapp.RequestHandler):
"""
"""
def post(self):
    if "image" in self.request.arguments():
        content = "<html><body>Error</body></html>"      
        form = self.request.params['image']
        logging.info(form)
        try:
            filename = form.filename
            siz = len(form.value)

            ###--- Show data
            logging.info("Filename: %s" % form.filename)
            logging.info("Size: %s" % siz)

            content = "<html><body>File has been uploaded Name: %s Size: %s</body></html>" % (filename, siz)
            logging.info("DONE")
        except:     
            logging.info("Error: bad form or something like that")

        logging.info("writing out response")
        self.response.out.write(content)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莫相离 2024-08-13 12:25:42

在请求完成之前,App Engine 不会从请求处理程序返回任何数据,因此任何进度检查都必须在客户端完成。 IIRC 在上传完成之前,您的请求甚至不会加载到应用程序服务器中。

App Engine won't return any data from the request handler until the after the request is completed, so any progress checking would have to be done client-side. IIRC your request won't even be loaded into the application server until the upload is complete.

夏见 2024-08-13 12:25:42

您可以使用 swfupload 限制客户端网站上的文件大小。

它是一个带有 javascript api 的 Flash 组件,可公开 Flash 上传功能。
文件大小的检查是在客户端浏览器中完成的。 (在函数 CheckFileSize)

该库还有其他好东西,例如进度条,请查看其主页以获取完整的功能列表。

You can limit file size on a client site using swfupload.

It's a flash component with javascript api that exposes flash uploading capabilities.
The check of a file size is done in the client browser. (in function CheckFileSize)

The library has also other goodies like progress bar, check it home page for full feature list.

夜夜流光相皎洁 2024-08-13 12:25:42

这并不适用于所有浏览器,但它是通过 AJAX 进行多个文件上传的好方法:

function upload_files(entityKey, files, url, progress_callback) {
  var xhr = new XMLHttpRequest(), formData = new FormData();
  xhr.upload['onprogress'] = progress_callback;

  formData.append('entityKey', entityKey);
  $.each(files, function(i, file) { formData.append('file[]', file);});

  xhr.open("post", url, true);
  xhr.setRequestHeader("Cache-Control", "no-cache");
  xhr.send(formData);
}

entityKey 是服务器上参数的示例。 'files' 参数来自文件类型输入表单元素的 'files' 属性(作为支持多个的数组)。 “progress_callback”参数是一个函数,它接受一个具有(至少)“已加载”和“总计”字段(单位为字节)的对象。它不关心服务器响应。

This won't work for every browser, but it's a nice way of doing multiple file uploads via AJAX:

function upload_files(entityKey, files, url, progress_callback) {
  var xhr = new XMLHttpRequest(), formData = new FormData();
  xhr.upload['onprogress'] = progress_callback;

  formData.append('entityKey', entityKey);
  $.each(files, function(i, file) { formData.append('file[]', file);});

  xhr.open("post", url, true);
  xhr.setRequestHeader("Cache-Control", "no-cache");
  xhr.send(formData);
}

The entityKey is an example of a parameter on the server. The 'files' parameter comes from the 'files' attribute of the file-type input form element (as an array to support multiple). The 'progress_callback' parameter is a function that takes an object that has (at least) a 'loaded' and a 'total' field (unit is bytes). It doesn't care about the server response.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文