如何在asp.net中设置.ashx文件的会话值?

时间:2013-04-24 15:26:00

标签: asp.net session file-upload listbox uploadify

我在我的网站上使用“jquery.uploadify.js”,这个jquery使用ashx文件将图像上传到文件夹中。在.ashx中,我使用Session [“FileNameNews”]来保存图像名称,我在我的代码开头清空了Session [“FileNameNews”]。但是当我上传两个或三个或......图像时,每次我的Session [“FileNameNews”]都是空的。每次上传照片时我都不希望自己的会话空白,我希望上传的图像显示在父.aspx页面的列表框中。其他方法,我需要在uplaod开始时将我的会话清空,并在上传结束时填写图像名称。我一次可以上传多张图片。

有没有人有想法?请帮助我。

谢谢。

.aspx页面:

<script type = "text/javascript">
    $(window).load(
        function() {
            $("#<%=FileUpload1.ClientID%>").fileUpload({
            'uploader': 'scripts/uploader.swf',
            'cancelImg': 'images/cancel.png',
            'buttonText': 'Browse Files',
            'script': 'Upload.ashx',
            'folder': 'Temp',
            'fileDesc': 'Image Files',
            'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
            'multi': true,
            'auto': false
        });
       }
    );
    </script>


<a href="javascript:$('#<%=FileUpload1.ClientID%>').fileUploadStart()">Start Upload</a> 
|<a href="javascript:$('#<%=FileUpload1.ClientID%>').fileUploadClearQueue()">Clear</a> 
<div style = "padding:40px">
  <asp:FileUpload ID="FileUpload1" runat="server" />
</div> 

和Upload.ashx:

public class Upload : IHttpHandler, IRequiresSessionState {

public void ProcessRequest(HttpContext context)
{
    context.Session["FileNameNews"] = "";
    context.Response.ContentType = "text/plain";
    context.Response.Expires = -1;
    try
    {
        HttpPostedFile postedFile = context.Request.Files["Filedata"];

        string savepath = "";
        string tempPath = "";
        tempPath = "Temp";//System.Configuration.ConfigurationManager.AppSettings["FolderPath"]; 
        savepath = context.Server.MapPath(tempPath);
        string filename = postedFile.FileName;
        if (!Directory.Exists(savepath))
            Directory.CreateDirectory(savepath);

        string SitePath = context.Server.MapPath(context.Request.ApplicationPath) + @"\Temp\";
        string SitePath1 = context.Server.MapPath(context.Request.ApplicationPath) + @"\WebImages\NewsImages\";
        string FileN = SitePath + filename + "{---}" + context.Session["UserID"].ToString();
        if ((File.Exists(SitePath + filename + "{---}" + context.Session["UserID"])) || (File.Exists(SitePath1 + filename)))
        {
            return;
        }
        else
        {
            postedFile.SaveAs(savepath + @"\" + filename);
            postedFile.SaveAs(savepath + @"\" + filename + "{---}" + context.Session["UserID"]);
            if (context.Session["FileNameNews"] == "") { context.Session["FileNameNews"] = filename; }
            else { context.Session["FileNameNews"] = context.Session["FileNameNews"] + "," + filename; }
            context.Response.Write(tempPath + "/" + filename);
            context.Response.StatusCode = 200;
        }
    }
    catch (Exception ex)
    {
        context.Response.Write("Error: " + ex.Message);
    }
}

public bool IsReusable
{
    get
    {
        return false;
    }
}
}

1 个答案:

答案 0 :(得分:1)

如果此ashx处理程序是文件上传ajax请求的端点,并且您抛出异常,则永远不会看到来自try / catch的错误消息。您可能会“吞下”您的例外情况。删除try / catch并使用浏览器中的调试工具检查结果状态(或错误消息)。