jQuery plugin uploadify for ajax effect picture upload

  • 2021-06-29 09:35:57
  • OfStack

Yesterday, I uploaded the picture of ajax effect for 1 day, just to learn more precisely, so I read a lot of third-party controls, and finally chose uploadify control, mainly because it is easier to get started.

First let's refer to other people's data (I organized it myself)

Optional

需要参数类型 参数名字 解释
(布尔型) auto 当文件被添加到队列时,自动上传。
(字符串) buttonImg 浏览按钮的背景图片路径。
(字符串) buttonText 默认在按钮上显示的文本。
(字符串) cancelImg 取消按钮的背景图片路径。
(字符串) checkScript 用以检查服务器上已存在文件的后台脚本的路径。【译者注:应该是ajax方式】
(字符串) displayData 在上传过程中显示在队列里的数据类型。
(字符串) expressInstall expressInstall.swf文件的路径。
(字符串) fileDataName 后台脚本中要处理的file域的名称。【译者注:就是type为file的input的name值】
(字符串) fileDesc 在浏览窗口底部的文件类型下拉菜单中显示的文本。
(字符串) fileExt 允许上传的文件后缀。【译者注:.jpg/.png等】
(字符串) folder 上传文件夹的路径,文件上传后将被保存于此。
(整型) height 浏览按钮的高度。
(布尔型) hideButton 设置为true将隐藏flash按钮,这样你就可以为下面的div元素定义样式。
(字符串) method 向后台脚本放送数据的表单方法。
(布尔型) multi 设置为true将允许多文件上传。
(字符串) queueID 页面中,你想要用来作为文件队列的元素的id。
(整型) queueSizeLimit 上传队列中所允许的文件数量。
(布尔型) removeCompleted 设置为true将自动移除队列中已经完成上传的项目。
(布尔型) rollover 设置为true将激活浏览按钮的鼠标划过状态。
(字符串) script 处理文件上传的后台脚本的路径。
(字符串) scriptAccess 设置在主swf文件中的脚本访问模式。
(JSON) scriptData 在文件上传时,应该被发送给后台脚本的1个包含name/value键值对以及1些额外信息的json对象。
(整型) simUploadLimit 允许同时上传的文件数量。
(整型) sizeLimit 上传文件的大小限制,单位为字节。
(字符串) uploader uploadify.swf文件的路径。
(整型) width 浏览按钮的宽度。
(字符串) wmode flash文件的wmode。

Event

(函数) onAllComplete 当上传队列中的所有文件都完成上传时触发。
(函数) onCancel 当从上传队列每移除1个文件时触发1次。
(函数) onCheck 在上传开始之前,如果检测到1个同名文件时触发。
(函数) onClearQueue 当uploadifyClearQueue()方法被调用时触发。
(函数) onComplete 每完成1次文件上传时触发1次。
(函数) onError 当上传返回错误时触发。
(函数) onInit 当Uploadify实例被载入时触发。
(函数) onOpen 当上传队列中的1个文件开始上传时就触发1次。
(函数) onProgress 在上传过程中触发。
(函数) onQueueFull 当文件数量达到上传队列限制时触发。
(函数) onSelect 每向上传队列添加1个文件时触发。
(函数) onSelectOnce 每向上传队列添加1个或1组文件时触发。
(函数) onSWFReady 当flash文件载入完成时触发。

Method

.uploadify() 创建Uploadify上传组件的1个实例。
.uploadifyCancel() 从上传队列移除1个文件。如果文件正在上传,该方法将先取消上传,然后再将文件移除出上传队列。
.uploadifyClearQueue() 将所有文件移除出上传队列,并且取消正在执行的所有上传。
.uploadifySettings() 改变Uploadify组件的可选参数。
.uploadifyUpload() 触发上传。

You'll use some parameters here, but we don't need to have a complete understanding of them. Before you use them, you'll see which parameters you'll use, ok.

Due to time constraints, I will send out the core code for your reference:

stay < head > Apply css style to label first < link href="../Plugin/uploadify.css" rel="stylesheet" type="text/css" / > Path Here you may not be the same as mine

Next on < head > Use js in the label, js code should be in order or error will occur.


<script type="text/javascript" src="../js/jquery-1.4.2.min.js"></script>
<script src="../Plugin/swfobject.js" type="text/javascript"></script>
<script src="../Plugin/jquery.uploadify.v2.1.4.min.js" type="text/javascript"></script>

The same path here is to change to your own.uploaddiy was developed with jquery, so we need to apply jquery before we can use uploaddiy, so we should pay attention to the sequence when applying js

Next comes the core html code


<tr>
<th scope="row"> chart   Picture: </th>
<td>
 * <div style="float:left;width:125px;height:35px;">
  * <input type="file" name="uploadify" id="uploadify" /> <%-- Upload progress bar --%>
 </div>
 <div id="fileQueue" style="float:left;height:35px;"></div>
 <div style="float:left;height:35px;">
  * <a href="javascript:$('#uploadify').uploadifyUpload()" class="btn-lit"><span> upload </span></a>
  <a href="javascript:$('#uploadify').uploadifyClearQueue()" class="btn-lit"><span> Cancel upload </span></a>
 </div>
</td>
</tr>
<tr>
  <th scope="row">&nbsp;</th>
  <td><asp:Image ID="pic" runat="server" /></td> * <%-- Successful Upload Generate Picture Preview --%>
</tr>

Write in table table of form

Then we start to finish the interface code that the control gives us to finish as follows:


//uploadify Custom settings for plug-ins 
$(document).ready(function () {
 $("#uploadify").uploadify({
  'uploader': '../Plugin/uploadify.swf',
  'script': 'UploadImg.ashx',
  'cancelImg': '../Plugin/cancel.png',
  'folder': '../upload',
  'buttonText': 'SELECT Pic',
  'fileExt': '*.jpg;*.gif,*.png',     // File format allowed to upload is *.jpg,*.gif,*.png
  'fileDesc': 'Web Image Files(.JPG,.GIF,.PNG)', // Filter out *.jpg,*.gif,*.png Files 
  'queueID': 'fileQueue',
  'sizeLimit': '2048000',       // The maximum allowable file size is 2M
  'auto': false,         // Require manual application submission 
  'multi': false,         //1 Only uploads allowed 1 Picture 
  'onCancel': funCancel,       // When the user cancels the upload 
  'onComplete': funComplete,      // Complete upload task 
  'OnError': funError        // When an upload error occurs 
 });
});

// User Cancel Function 
function funCancel(event, ID, fileObj, data) {
 jBox.tip(' You canceled ' + fileObj.name + ' operation ', 'info');
 return;
}

// What happened when the picture was uploaded 
function funComplete(event, ID, fileObj, response, data) {
 //$("#pic").html('<img id="pic" runat="server" src=../upload/' + response + '.jpg style="width:300;height:200px;"/>');
 if (response == 0) {
  jBox.tip(' picture ' + fileObj.name + ' operation failed ', 'info');
  return;
 }
 $("#pic").attr("src", "../upload/" + response).height(200).width(300);
 jBox.tip(' picture ' + fileObj.name + ' Operation Successful ', 'info');
 return;
}

// When an upload error occurs. 
function funError(event, ID, fileObj, errorObj) {
 jBox.tip(errorObj.info);
 return;
}

Next, let's finish the general processing time file with the following code:


context.Response.ContentType = "text/plain";
context.Response.Charset = "utf-8";

HttpPostedFile file = context.Request.Files["Filedata"];
string uploadPath = HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";

if (file != null)
{
 //if (File.Exists(uploadPath + file.FileName))
 //{
 // context.Response.Write("3");   // file already exists 
 // return;
 //}

 string[] fn = file.FileName.Split('.');
 string ext = fn[fn.Length - 1];
 string filename = DateTime.Now.ToString("yyyyMMddhhmmss")+"."+ext;
 if (!Directory.Exists(uploadPath))
 {
  Directory.CreateDirectory(uploadPath);
 }
 file.SaveAs(uploadPath + filename);
 // If the code below is missing, the display of the upload queue will not disappear automatically after the upload is successful 
 context.Session[context.Session["userName"].ToString()] = filename;
  
 context.Response.Write(filename);
}
else
{
 context.Response.Write("0");
} 

Successfully uploaded the name of the returned file, if failed, returns a 0, js does not pursue the return value, if 0 means failure, if not 0 then dynamically loads src to img.

Source download: http://xiazai.ofstack.com/201606/yuanma/jQuery-uploadify-ajax-upload (ofstack.com).rar


Related articles: