如何从.ashx发送JSON响应以上传onUploadSuccess

时间:2014-04-08 03:49:53

标签: jquery asp.net json uploadify

我需要响应来自.ashx文件的字符串来上传`onUploadSuccess'读取消息。

如何将响应字符串发送到uploadify?

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);

1 个答案:

答案 0 :(得分:0)

您可以使用其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);
            }
        });
    })
);

Handler.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;
},