我需要从 javascript 将图像上传到网络服务。我必须发送一个 json 字符串和一个文件(图像)。在java中我们有MultipartEntity
。我在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 );
现在我需要在 javascript 中执行相同的图像上传。
但在 javaScript 中我没有找到任何与 MultipartEntity 等效的东西。请提出任何解决方案。
为了上传图像,我使用Valum的ajax上传插件 http://valums.com/ajax-upload/ or jQuery 表单插件 http://jquery.malsup.com/form/#getting-started允许以 ajax 方式提交普通表单。
如果您将使用 POST 请求,那么不要忘记使用 MAX_FILE_SIZE 隐藏属性:
<input type="hidden" name="MAX_FILE_SIZE" value="20000000">
请注意,它必须位于文件输入字段之前。它以字节为单位,因此这会将上传限制为 20MB。看PHP 文档 http://www.php.net/manual/en/features.file-upload.post-method.php了解详情。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)