您现在的位置是:首页 > 文章详情

fineuploader使用实例

日期:2014-10-24点击:708

1、Fine Uploader特点

Fine Uploader Features:

A:支持文件上传进度显示.

B:文件拖拽浏览器上传方式

C:Ajax页面无刷新.

D:多文件上传.

F:跨浏览器.

E:跨后台服务器端语言.

2、页面前端代码

 <div id="btnUpload"></div>

 $(function () { $('#btnUpload').fineUploader({ request: { endpoint: 'Handler/UpLogo.ashx' }, validation: { allowedExtensions: ['jpeg', 'jpg', 'png'] }, multiple: false, text: { uploadButton: '<div>上传头像</div>' } }).on('complete', function (event, id, fileName, responseJson) { if (responseJson.success) { $("#absoluteUrl").val(responseJson.path); $("#jcrop_target").attr("src", responseJson.url); $("#jcrop_target").css({ "width": 300, "height": 300 }); $("#preview").attr("src", responseJson.url); $("#preview").css({ "width": 100, "height": 100 }); ias.update(); } }); });


2、Handler文件代码

<%@ WebHandler Language="C#" Class="UpLogo" %> using System; using System.Web; using System.IO; using System.Text; using System.Net; using System.Data; using System.Collections; using System.Collections.Generic; using System.Drawing; using ECS.Utility; public class UpLogo : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Request != null && context.Request.Files.Count > 0) { var HttpFile = context.Request.Files[0]; var allowedExt = new List<string> { ".jpg", ".gif", ".bmp", ".png" }; var fileExt = Path.GetExtension(HttpFile.FileName).ToLower(); var File_Name = Path.GetFileNameWithoutExtension(HttpFile.FileName); var toFileName = Guid.NewGuid().ToString() + fileExt; var toFileFullPath = context.Server.MapPath("~/UpFiles/UserTemFace/"); var viewPath = "/UpFiles/UserTemFace/"; var Title = File_Name; DirectoryInfo di = new DirectoryInfo(toFileFullPath); if (!di.Exists) { di.Create(); } ECS.Model.A_User Model = new ECS.BLL.A_User().GetModel(ValUtil.GetUserID()); if (Model != null) { //toFileName = Model.UserFaceImg; string saveFile = toFileFullPath + toFileName; //先删除临时文件 //var _filePath = toFileFullPath + "\\" + toFileName; //if (File.Exists(_filePath)) //{ // FileInfo fi = new FileInfo(_filePath); // if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1) // { // fi.Attributes = FileAttributes.Normal; // } // File.Delete(_filePath); //} HttpFile.SaveAs(saveFile); Model.UserFaceImg = toFileName; new ECS.BLL.A_User().Update(Model); } string imgeUrl = ""; if (fileExt.ToLower().Equals(".jpg") || fileExt.ToLower().Equals(".gif") || fileExt.ToLower().Equals(".bmp") || fileExt.ToLower().Equals(".png")) { imgeUrl = viewPath + toFileName; } else { context.Response.Write("{success:false,msg:'只能上传图片类型的文件'}"); context.Response.End(); return; } string url = viewPath + "/" + toFileName; context.Response.Write(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(new { success = true, url = url, path = toFileName })); context.Response.End(); } else { context.Response.Write(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(new { success = false })); context.Response.End(); } } public bool IsReusable { get { return false; } } }


原文链接:https://yq.aliyun.com/articles/248163
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章