开发者

Google appengine blobstore upload handler processing extra form post parameters

开发者 https://www.devze.com 2023-01-29 06:31 出处:网络
I wish to have a file upload form that in addition to the file selection input , also has other input fields like textarea, dropdown etc. The problem is that I cannot access any post parameters other

I wish to have a file upload form that in addition to the file selection input , also has other input fields like textarea, dropdown etc. The problem is that I cannot access any post parameters other than the file in my blobstore upload handler. I am using the following function call to get the parameter name but it always returns an empty screen.

par = self.request.get("par")

I found another question with a similar problem Uploading a video to google app engine blobstore. The answer to that question suggests a workaround sol开发者_StackOverflow社区ution to set the filename to the parameter you wish to read which is not sufficient for my needs. Is there a way to access other form parameters in the post method of blobstore upload handler?


Did you find the solution?

In my experience, when using form/multipart request doesn't include the other parameters and they have to be dug out manually.

This is how I dig out parameters out of request that is used to send a file.

import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;

// for reading form data when posted with multipart/form-data
import java.io.*;
import javax.servlet.ServletException; 
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.google.appengine.api.datastore.Blob; 


// Fetch the attributes for a given model using rails conventions.
// We need to do this in Java because getParameterMap uses generics.
// We currently only support one lever: foo[bar] but not foo[bar][baz].
// We currently only pull the first value, so no support for checkboxes
public class ScopedParameterMap {
  public static Map params(HttpServletRequest req, String model) 
  throws ServletException, IOException {        

      Map<String, Object> scoped = new HashMap<String, Object>();      

      if (req.getHeader("Content-Type").startsWith("multipart/form-data")) {
        try { 
          ServletFileUpload upload = new ServletFileUpload();
          FileItemIterator iterator = upload.getItemIterator(req); // this is used to get those params

          while (iterator.hasNext()) {
            FileItemStream item = iterator.next(); 
            InputStream stream = item.openStream(); 

            String attr = item.getFieldName();

            if (attr.startsWith(model + "[") && attr.endsWith("]")) { // fetches all stuff like article[...], you can modify this to return only one value
              int len = 0;
              int offset = 0;
              byte[] buffer = new byte[8192];
              ByteArrayOutputStream file = new ByteArrayOutputStream();

              while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
                offset += len;
                file.write(buffer, 0, len);
              }

              String key = attr.split("\\[|\\]")[1];

              if (item.isFormField()) {
                scoped.put(key, file.toString());
              } else {
                if (file.size() > 0) {
                  scoped.put(key, file.toByteArray());
                }
              }
            }
          }
        } catch (Exception ex) { 
          throw new ServletException(ex); 
        }
      } else {
        Map params = req.getParameterMap();
        Iterator i = params.keySet().iterator();
        while (i.hasNext()) {
            String attr = (String) i.next();
            if (attr.startsWith(model + "[") && attr.endsWith("]")) {
                String key = attr.split("\\[|\\]")[1];
                String val = ((String[]) params.get(attr))[0];
                scoped.put(key, val);
                // TODO: when multiple values, set a List instead
              }
            }
          }

      return scoped;
    }
  }

I hope this speedy answer helps, let me know if you have questions.

0

精彩评论

暂无评论...
验证码 换一张
取 消