博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AjaxFileUpload + *.ashx 文件上传在IE8.0(XP,VS2010,Development Server)下的注意
阅读量:6801 次
发布时间:2019-06-26

本文共 3612 字,大约阅读时间需要 12 分钟。

ashx,中返回类型设置成 json,plain时,返回的Json代码解析都出错,调试发现实际返回的除了json字符串外还有,其他html或xml导致无法解析成json对象

function UploadFile(elmFlag) {            $.ajaxFileUpload({                url: '/FileUpload.ashx',                secureuri: false,                fileElementId: "fu" + elmFlag,                dataType: "json",                success: function (data, status) {                    if (data.Code == 200) {                        alert( unescape(data.Name));                    } else {                        alert( unescape(data.Msg));                    }                    $("#c_" + elmFlag).empty();                    $("#c_" + elmFlag).append("");                },                error: function (data, status, e) {                    alert("错误:上传组件错误,请检察网络!");                }            });        }

 服务器端

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Script.Serialization;using System.IO;using System.Web.Hosting;using F.Studio.Common;using Microsoft.Practices.Unity;using JLMFG.Doc.BLL;using JLMFG.Doc.Domain;namespace JLMFG.Doc.Web{    ///     /// FileUpload 的摘要说明    ///     public class FileUpload : IHttpHandler,IBuildUp    {        static readonly string C_FileRoot = "/Files/";        [Dependency]        public doc_DocManager DocMgr { get; set; }        private HttpRequest Request { get;  set; }        private HttpResponse Response { get; set; }        private JavaScriptSerializer Serializer { get; set; }        public void ProcessRequest(HttpContext context)        {            Request = context.Request;            Response = context.Response;            Serializer = new JavaScriptSerializer();            Response.ContentType = "text/html";            Response.Expires = -1;            try            {                if (Request.Files.Count <= 0) throw new Exception("没有文件上传!");                             SaveFile();            }            catch (Exception ex)            {                var resp=new UploadResponse(){Code=400,Msg=StringHelper.Escape(ex.Message)};                Response.Write(Serializer.Serialize(resp));            }            Response.End();                   }        private void  SaveFile()        {            var file = Request.Files[0];            var ext = Path.GetExtension(file.FileName);            //确保目录存在            string path =  C_FileRoot + DateTime.Now.ToString("yyyy-MM-dd") + "/";            if (!Directory.Exists( HostingEnvironment.MapPath(path)))            {                Directory.CreateDirectory(HostingEnvironment.MapPath(path));            }            //合成文件名            var filename= path + Guid.NewGuid().ToString("N").Substring(0,8) + ext;            var resp = new UploadResponse();            resp.MIME = file.ContentType;            resp.Size = file.ContentLength / 1024;            resp.Name =StringHelper.Escape( Path.GetFileNameWithoutExtension(file.FileName));            resp.Path =StringHelper.Escape( filename);            resp.Code = 200;            resp.Msg = "Success";            //保持文件            file.SaveAs(System.Web.Hosting.HostingEnvironment.MapPath(filename));                 Response.Write(Serializer.Serialize(resp));        }        public bool IsReusable        {            get            {                return false;            }        }        public class UploadResponse        {            public int Code { get; set; }            public string Msg { get; set; }            public string Path { get; set; }            public string Name { get; set; }            public long Size { get; set; }            public string MIME { get; set; }        }    }}

 

转载地址:http://gxjwl.baihongyu.com/

你可能感兴趣的文章
CEPH Cache Tiering
查看>>
Oracle 11g新特性之--Server Result Cache
查看>>
Oracle中的ORA-01548: active rollback segment '_SYSSMU1$' found
查看>>
AngularJs $anchorScroll、$controller、$document
查看>>
Microsoft资源
查看>>
WordPress 永久链接或固定链接设置技巧
查看>>
数据结构之线性表
查看>>
在PPT中插入FLASH遇到的系列问题
查看>>
XSS研究4-来自外部的XSS攻击的防范
查看>>
Spring知识点总结-1
查看>>
微软私有云分享(R2)21 BMC提升B格
查看>>
MDSF:如何使用GMF来做TOGAF建模工具
查看>>
Spring Security简介
查看>>
打造一流的研发中心
查看>>
MCollective架构篇3-Puppet插件的部署及测试
查看>>
配置GNS使用CRT连接
查看>>
Java:集合类性能分析
查看>>
cms无法登陆
查看>>
JavaScript中事件处理
查看>>
VSTO 向office文档中插入内容
查看>>