栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

jQuery Uploadify 上传插件出现Http Error 302 错误的解决办法

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

jQuery Uploadify 上传插件出现Http Error 302 错误的解决办法

前段时间介绍过jquery uploadify上传插件的使用方法,我在使用中遇到过Http Error 302错误问题,应该会有很多人在使用中遇到过,在此记录下来:

首先http 302是请求被重定向的意思,这就很容易理解了,如果你的uploadify处理上传脚本有session验证,就会出现此错误,因为flash在执行post请求的时候没有包含cookie信息,而服务器的session会根据客户端的cookie来得到SESSIONID。没有提交cookie自然就不能获取到session,然后uploadify就返回了302(请求被重定向)的错误。

解决办法当然是把session_id的值传到服务端:

 

然后在服务器端session验证之前:

if (isset($_POST['session'])){ 
  session_id($_POST['session']); 
  session_start();//注意此函数要在session_id之后 
} 

当然,你也可以直接在url中将session id传过去。

yii中代码如下:

$('#').uploadify({
      'buttonText': '选择文件..',
      'fileObjName': 'imgFile',
      'method': 'post',
      'multi': false,
      'queueID': 'fileQueue',
      
      'fileTypeExts': '*.gif;*.png;*.jpg;*.bmp;*.jpeg;',
      'buttonImage': '_static_public?>/js/uploadify/select.png',
      'formData': {
 'sessionId'  : 'session->sessionID; ?>',
 'timestamp'  : '',
 'token'    : '',
 'modelName' : '',
 'modelId' : 'id; ?>'
      },
      'swf': '_static_public;?>/js/uploadify/uploadify.swf',
      'uploader': 'createUrl('uploadify/basicExecute')?>',
      'onUploadStart': function () {
 $('# img').remove();
 $('# a').remove();
 $imgHtml = '';
 $('#').append($imgHtml);
      },  
      'onUploadSuccess': function(file, data, response) {
 $('.upload_load').remove(); 
 var json = $.parseJSON(data); 
 if (json.state == 'success') {
   $("#").remove();
   $(yt_upload_name_id).val(json.fileId);
   $imgHtml ='">';   
   $imgHtml += '_baseUrl?>/' + json.file + '" target="_blank">';
   $imgHtml += '_baseUrl?>/'+json.file+'" width="85" height="75" align="absmiddle"/>';
   $imgHtml += '';
   $imgHtml += '","")">删除';
   $imgHtml +='';
   $('#').append($imgHtml);
 } else {
   alert(json.message);
 }
      },
      'onQueueComplete':function () {
 $('.upload_load').remove();
      }
    }); 

服务端:

if (isset($_POST['sessionId'])) {
  $session = Yii::app()->getSession();
  $session->close();
  $session->sessionID = $_POST['sessionId'];
  $session->open();
}

ps:jquery上传插件uploadify使用心得(总结)

自己使用实例:

1、jsp页面:





//jquery文件上传
$(document).ready(function()
    {
      $("#uploadify").uploadify({
 'uploader': 'jsp/js/jquery_upload/uploadify.swf',
 'script': 'uploadFile.svl',
 'cancelImg': 'jsp/js/jquery_upload/cancel.png',
 'queueID': 'fileQueue',
 'auto': false,
 'multi': true,
 'method':'POST',
 'scriptData':{'saveFolder':'stuPhotos'},//GET方式才可生效
 'fileExt' :'*.jpg;*.gif;*.png', //控制可上传文件的扩展名
 'fileDesc': 'jpg、gif、png文件', //控制可上传文件的扩展名描述,两者需要同时使用 
 'buttonImg':'jsp/js/jquery_upload/selectBtn.gif',
 'width':80,//"浏览"按钮宽度
 'onComplete':function(event,ID,fileObj,response,data){
  //alert(response) //response为服务器响应数据
 },
      });
}); 
照片:


上传| 取消上传

2、服务端代码

public class UploadFileUtil extends HttpServlet {
private static final long serialVersionUID = 1L;
File tmpDir = null;// 初始化上传文件的临时存放目录
File saveDir = null;// 初始化上传文件后的保存目录
public UploadFileUtil() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  try{
    if(ServletFileUpload.isMultipartContent(request)){
     response.setCharacterEncoding("utf-8");//务必,防止返回文件名是乱码 
     DiskFileItemFactory dff = new DiskFileItemFactory();//创建该对象
     dff.setRepository(tmpDir);//指定上传文件的临时目录
     dff.setSizeThreshold(1024000);//指定在内存中缓存数据大小,单位为byte
     ServletFileUpload sfu = new ServletFileUpload(dff);//创建该对象
     sfu.setFileSizeMax(5000000);//指定单个上传文件的最大尺寸
     sfu.setSizeMax(10000000);//指定一次上传多个文件的总尺寸
     FileItemIterator fii = sfu.getItemIterator(request);//解析request 请求,并返回FileItemIterator集合
     while(fii.hasNext()){
      FileItemStream fis = fii.next();//从集合中获得一个文件流
      if(!fis.isFormField() && fis.getName().length()>0){//过滤掉表单中非文件域
String fileName = fis.getName();//获取文件名
String extName = "";
if (fileName.lastIndexOf(".") >= 0) {
extName = fileName.substring(fileName.lastIndexOf("."));
}
 BufferedInputStream in = new BufferedInputStream(fis.openStream());//获得文件输入流
 String uuidName = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();//用UUID生成文件名
 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(saveDir+"/"+uuidName+extName)));//获得文件输出流
 Streams.copy(in, out, true);//开始把文件写到你指定的上传文件夹
      }
     }
     //jquery上传方式返回
     response.getWriter().print("upload success");//成功
    }
  }catch(Exception e){
   response.getWriter().print("upload fail");//失败
    e.printStackTrace();
  }
 } public void init() throws ServletException {
  super.init();
  String serverPath = this.getServletConfig().getServletContext().getRealPath("/");//获取服务器路径
   String tmpPath = serverPath+"/tmpUploadsFolder/";
   String savePath = serverPath+"/uploadsFolder/";
  tmpDir = new File(tmpPath);
  saveDir = new File(savePath);
  if(!tmpDir.isDirectory())
    tmpDir.mkdir();
  if(!saveDir.isDirectory())
    saveDir.mkdir();
 }}

以上内容是小编给大家介绍的jQuery Uploadify 上传插件出现Http Error 302 错误的解决办法,希望大家喜欢。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/99432.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号