图像上传到JavaScript的Web服务 [英] image upload to web service in JavaScript
问题描述
我需要从JavaScript上传图像到web服务。我必须发送一个json字符串的一个文件(图片)。在Java中,我们有 MultipartEntity
。我在java中有以下代码:
HttpPost post = new HttpPost(aWebImageUrl2);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
//对于文件参数
entity.addPart(picture,new FileBody(((File)imgPath)));
//对于通常的字符串参数
entity.addPart(url,new StringBody(aImgCaption,text / plain,Charset.forName(UTF-8)));
post.setEntity(entity);
现在我需要在javascript中做同样的图片上传。 javaScript我没有找到任何等效的MultipartEntity。请推荐任何解决方案。
对于上传图片,我使用 Valum的ajax上传插件或 jQuery表单插件,允许以ajax的方式提交一个正常的表单。
如果您使用POST请求,那么不要忘记使用MAX_FILE_SIZE hidden属性: p>
< input type =hiddenname =MAX_FILE_SIZEvalue =20000000>
请注意,它必须在文件输入字段之前。它是以字节为单位的,所以这会限制上传到20MB。有关详细信息,请参阅 PHP文档。
I need to upload an image to a webservice from javascript. I have to send a json string an a file(image). In java we have MultipartEntity
. I have the followig code in java:
HttpPost post = new HttpPost( aWebImageUrl2 );
MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );
// For File parameters
entity.addPart( "picture", new FileBody((( File ) imgPath )));
// For usual String parameters
entity.addPart( "url", new StringBody( aImgCaption, "text/plain", Charset.forName( "UTF-8" )));
post.setEntity( entity );
Now I need to do the same image upload in javascript.
But in javaScript I didn't find any equivalent of MultipartEntity. Please Suggest any solutions.
For uploading images I use either Valum's ajax upload plugin or jQuery form plugin that allows to submit a normal form in an ajax way.
If you will use POST requests then don't forget to use MAX_FILE_SIZE hidden attribute:
<input type="hidden" name="MAX_FILE_SIZE" value="20000000">
Note that it must precede the file input field. It is in bytes, so this will limit the upload to 20MB. See PHP documentation for details.
这篇关于图像上传到JavaScript的Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!