【问题标题】:How to send JSON response from .ashx to uploadify onUploadSuccess如何从 .ashx 发送 JSON 响应以上传 onUploadSuccess
【发布时间】:2014-04-08 03:49:52
【问题描述】:

我需要从.ashx 文件中响应字符串来上传“onUploadSuccess”读取消息。

如何发送响应字符串进行上传?

If results = True Then
   Dim outputToReturn = [String].Format("{ ""msg"" : ""{0}""  }", "Success")
   context.Response.Write(outputToReturn)
   context.Response.StatusCode = 200

Else
   Dim outputToReturn = [String].Format("{ ""msg"" : ""{0}""  }", "Failed")
   context.Response.Write(outputToReturn)
   context.Response.StatusCode = 200
End If


 'onUploadSuccess': function (file, data, response) {
                alert(response);

【问题讨论】:

    标签: jquery asp.net json uploadify


    【解决方案1】:

    你可以使用它的 OnUploadComplete 事件来获取上传的图片:

    $(window).load(
        $(function () {
            $("#file_uploads").uploadify({
                'swf': '../Media/uploadify.swf',
                'uploader': '/WebServices/AttachmentUpload.ashx',
                'cancelImg': '../Media/cancel.png',
                'fileSizeLimit': '25MB',
                'buttonText': 'Attach Files',
                'fileTypeExts': '*.jpg;*.jpeg;*.gif;*.png;*.txt;*.pdf',
                'fileDesc': 'Image Files',
                'multi': true,
                'auto': true,
                'onUploadComplete': function (file) {
                    var attchVal = fileName;
                    $("#hdnAttachment").val(attchVal);
                }
            });
        })
    );
    

    处理程序.ashx

    <%@ WebHandler Language="C#" Class="AttachmentUpload" %>
    
    using System;
    using System.Web;
    using System.IO;
    using CRMBiz;
    using CRMWeb.Utilities.Context;
    
    public class AttachmentUpload : IHttpHandler
    {
    
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;
            try
            {
                HttpPostedFile postedFile = context.Request.Files["Filedata"];
    
                string savepath = "";
                string tempPath = "";
    
                tempPath = System.Configuration.ConfigurationManager.AppSettings["FolderPath"]; 
                savepath = context.Server.MapPath(tempPath);
                string filename = postedFile.FileName;
                if (!Directory.Exists(savepath))
                    Directory.CreateDirectory(savepath);
    
                postedFile.SaveAs(savepath + @"\" + filename);
                context.Response.Write(tempPath + "/" + filename);
                context.Response.StatusCode = 200;
            }
            catch (Exception ex)
            {
                context.Response.Write("Error: " + ex.Message);
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    

    或检查响应:

    onComplete: function(event, queueID, fileObj, response, data) {
        alert(response.responseText);
        return false;
    },
    

    【讨论】:

    • 是的,但是我们可以传递这些(文件、数据、响应)参数。所以我想使用响应参数检查状态
    • 是的,所以我想将来自服务器的响应字符串传递给参数response。在我的情况下,响应总是说'true`
    猜你喜欢
    • 2011-08-24
    • 2010-11-19
    • 2017-12-21
    • 2010-09-30
    • 2021-02-06
    • 1970-01-01
    • 2015-04-11
    • 2018-08-29
    • 1970-01-01
    相关资源
    最近更新 更多