jsp中使用jquery的ajaxfileupload插件怎么实现异步上传
2个回答
展开全部
<script type=text/javascript src=js/jquery.js</script
<script type=text/javascript src=js/ajaxfileupload.js</script
<!-- 执行上传文件操作的函数 --
<script type=text/javascript
function ajaxFileUpload(){
$.ajaxFileUpload({url:'update.do?method=uploader', //需要链接到服务器地址
secureuri:false,
fileElementId:'houseMaps', //文件选择框的id属性
dataType: 'xml', //服务器返回的格式,可以是json
success: function (data, status) //相当于java中try语句块的用法{$('#result').html('添加成功');},
error: function (data, status, e) //相当于java中catch语句块的用法{$('#result').html('添加失败');}});}</script</head<body<form method=post action=update.do?method=uploader enctype=multipart/form-data
<input type=file id=houseMaps name=houseMaps/
<input type=button value=提交 onclick=ajaxFileUpload()/</form<div id=result</div</body</html服务器代码:
public class UpdateAction extends DispatchAction {
public ActionForward uploader(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UpFormForm upFormForm = (UpFormForm) form;
FormFile ff = upFormForm.getHouseMaps();try {InputStream is = ff.getInputStream();
File file = new File(D:/ + ff.getFileName()); //指定文件存储的路径和文件名
OutputStream os = new FileOutputStream(file);
byte[] b = new byte[1024];
int len = 0;
while((len = is.read(b)) != -1){
os.write(b, 0, len);}os.close();
is.close();
} catch (Exception e) {
2016-02-02 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
jquery的AsyncFileUpload控件,就是用来对文件进行异步上传的,示例如下:
1、前台代码:
<ajaxToolkit:AsyncFileUpload OnClientUploadError="uploadError" OnClientUploadComplete="uploadComplete"
runat="server" ID="AsyncFileUpload1" Width="400px" UploaderStyle="Modern"
UploadingBackColor="#CCFFFF"
onuploadedcomplete="AsyncFileUpload1_UploadedComplete" />
2、后台代码:
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
AjaxControlToolkit.AsyncFileUpload fileUploader = sender as AjaxControlToolkit.AsyncFileUpload;
if (fileUploader != null && fileUploader.HasFile)
{
fileUploader.SaveAs(Server.MapPath("/Resource/UploadFile/" + fileUploader.FileName));
}
}
1、前台代码:
<ajaxToolkit:AsyncFileUpload OnClientUploadError="uploadError" OnClientUploadComplete="uploadComplete"
runat="server" ID="AsyncFileUpload1" Width="400px" UploaderStyle="Modern"
UploadingBackColor="#CCFFFF"
onuploadedcomplete="AsyncFileUpload1_UploadedComplete" />
2、后台代码:
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
AjaxControlToolkit.AsyncFileUpload fileUploader = sender as AjaxControlToolkit.AsyncFileUpload;
if (fileUploader != null && fileUploader.HasFile)
{
fileUploader.SaveAs(Server.MapPath("/Resource/UploadFile/" + fileUploader.FileName));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询