Google appengine blobstore上传处理程序处理额外的表单发布参数 [英] Google appengine blobstore upload handler processing extra form post parameters
问题描述
我希望有一个文件上传的形式,除了文件选择输入,还有其他输入字段,如textarea,下拉等。问题是,我不能访问除我的blobstore上传处理程序中的文件之外的任何后期参数。我使用下面的函数调用来获取参数名称,但它总是返回一个空的屏幕。
par = self.request.get(par)
我发现了另一个类似问题的问题将视频上传到谷歌应用引擎blobstore 。该问题的答案提出了一种解决方法,将文件名设置为您希望读取的参数,这不足以满足我的需求。有没有办法在blobstore上传处理程序的post方法中访问其他表单参数?解决方案
您是否找到解决方案?
根据我的经验,当使用form / multipart请求不包含其他参数时,他们必须手工挖出。
这是我如何从用于发送文件的请求中挖掘参数。
import java。 util.Map;
import java.util.HashMap;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
//用于在使用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;
//使用Rails约定为给定模型获取属性。
//我们需要在Java中执行此操作,因为getParameterMap使用泛型。
//我们目前只支持一个杠杆:foo [bar]而不是foo [bar] [baz]。
//我们目前只提取第一个值,所以不支持复选框
public class ScopedParameterMap {
public static Map params(HttpServletRequest req,String model)
throws ServletException,IOException {
地图< String,Object> scoped = new HashMap< String,Object>();
$ b $ if(req.getHeader(Content-Type)。startsWith(multipart / form-data)){
try {
ServletFileUpload upload = new ServletFileUpload() ;
FileItemIterator iterator = upload.getItemIterator(req); //这是用来获得这些params
while(iterator.hasNext()){
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
String attr = item.getFieldName();
if(attr.startsWith(model +[)& attr.endsWith(])){//获取文章[...]之类的所有内容,您可以修改这只返回一个值
int len = 0;
int offset = 0;
byte [] buffer = new byte [8192];
ByteArrayOutputStream file = new ByteArrayOutputStream(); ((len = stream.read(buffer,0,buffer.length))!= -1){
offset + = len;
while
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:当有多个值时,设置一个List而不是
}
}
}
return scoped;
}
}
我希望这个快速回答有帮助,让我知道如果你有问题。
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 solution 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.
这篇关于Google appengine blobstore上传处理程序处理额外的表单发布参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!