Google appengine blobstore upload handler handles additional form message options

I want to have a file upload form that, in addition to inputting file input, also has other input fields such as textarea, dropdown, etc. The problem is that I cannot access any message parameters other than the file in my blobstore block loader. I use the following function call to get the parameter name, but it always returns a blank screen.

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

I found another question with a similar problem Uploading video to a blobstore device for a Google browser . The answer to this question offers a workaround for setting the file name to the parameter you want to read, which is not enough for my needs. Is there a way to access other form parameters in the post method for the blobstore upload handler?

+3
source share
1 answer

Did you find a solution?

In my experience, when using the form / multipart request, other options are not included, and you need to dig them out manually.

This is how I dig out the parameters from the request that is used to send the 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 quick answer helps, let me know if you have any questions.

+1

Source: https://habr.com/ru/post/1778764/


All Articles