How can I submit a text field in the POST request that uploads a blob to the Blobstore and retrieve it in the upload handler for the blob?

倖福魔咒の 提交于 2019-12-04 01:59:32
Ying Li

There's a way to post meta data with your blob with the information that would not be represented by a blobkey:

  1. In your upload form, include this:

    method="post" enctype="multipart/form-data"
    
  2. Now you can either add hidden fields:

    <input type="hidden" name="myName" value="<%= myName %>"/>
    
  3. Or you can add input fields:

    <input type="text" name="myText" size="50" value="Enter your text"/>
    
  4. In your servlet, make sure the post handler reads the meta-data

    String userName = req.getParameter("myName");
    
  5. Now you have the Upload form with all the information.

  6. When you are passing the information to serve the blob, you can use

    &blobkey=daffedafdfe&myName=Blah
    

So, you are not exactly storing the information in the blob itself, but you can include it in the upload form.

From what I can tell, the MultiPartEntityBuilder is respsonsible for mocking up an HTML "form" of enctype="multipart/form-data", and then by the time you get to the DefaultHttpClient lines, you are just sending the request from this form via POST. You should look into the documentaiton for the the entity builder. It has a function to add text fields to the "form"[1].

[1] - http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntityBuilder.html#addTextBody(java.lang.String,%20java.lang.String)

Just in case, you might want to specify

entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

after creating the object.

If that's not working, it might be advisable to post against requestbin to see exactly what you are receiving

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!