swfupload和asp.net接合需要注意的问题

如果直接运行官方所给的示例是能够成功运行的。但如果想运行在“无dll”文件的环境里面还需要注意以下几点:

1、页面session和flash session是不相同的,所以在操作的时候还要把Global.asax里面的内容放到工程下。因为这个示例是将上传的图片存放到内存中的,所以每次都要从session中取信息,如果不在request开始前进行操作,页面跟flash取到的是两个不同的会话,这就会成就图片上传成功,但看不到上传的内容。

void Application_BeginRequest(object sender, EventArgs e)
    {
        /* Fix for the Flash Player Cookie bug in Non-IE browsers.
         * Since Flash Player always sends the IE cookies even in FireFox
         * we have to bypass the cookies by sending the values as part of the POST or GET
         * and overwrite the cookies with the passed in values.
         *
         * The theory is that at this point (BeginRequest) the cookies have not been read by
         * the Session and Authentication logic and if we update the cookies here we’ll get our
         * Session and Authentication restored correctly
         */

        try
        {
            string session_param_name = "ASPSESSID";
            string session_cookie_name = "ASP.NET_SESSIONID";

            if (HttpContext.Current.Request.Form[session_param_name] != null)
            {
                UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]);
            }
            else if (HttpContext.Current.Request.QueryString[session_param_name] != null)
            {
                UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]);
            }
        }
        catch (Exception)
        {
            Response.StatusCode = 500;
            Response.Write("Error Initializing Session");
        }

        try
        {
            string auth_param_name = "AUTHID";
            string auth_cookie_name = FormsAuthentication.FormsCookieName;

            if (HttpContext.Current.Request.Form[auth_param_name] != null)
            {
                UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]);
            }
            else if (HttpContext.Current.Request.QueryString[auth_param_name] != null)
            {
                UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]);
            }

        }
        catch (Exception)
        {
            Response.StatusCode = 500;
            Response.Write("Error Initializing Forms Authentication");
        }
    }
    void UpdateCookie(string cookie_name, string cookie_value)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
        if (cookie == null)
        {
            cookie = new HttpCookie(cookie_name);
            HttpContext.Current.Request.Cookies.Add(cookie);
        }
        cookie.Value = cookie_value;
        HttpContext.Current.Request.Cookies.Set(cookie);
    }

当然,另外还需要通过ASPSESSID参数将sessionid传递给服务端

post_params : {
    "ASPSESSID" : "<%=Session.SessionID %>"
},

2、如果将Thumbnail.cs的内容改成Thumbnail.txt然后分别引入到upload.aspx和thumbnail.aspx中,也会造成问题,因为两个页面对应是并不是同一个类,所以会造成转换失败同样取不到所上传的图片。解决方法可以是将upload.aspx和thumbnail.aspx两个页面的内容合并,也可以是使用App_Code。

3、将文件保存目录中,通过Directory.Exists来判断目录是否存在,通过Directory.CreateDirectory来创建目录

HttpPostedFile file_upload = Request.Files["Filedata"];

display_file_name = file_upload.FileName;
file_size = file_upload.ContentLength;

string dir = DateTime.Now.ToString("yyyyMMdd");
string filename = DateTime.Now.ToString("yyyyMMddHHmmssfff");

string serverpath = Server.MapPath("file/") + dir + "/";
if (!Directory.Exists(serverpath))
{
    DirectoryInfo dirinfo = Directory.CreateDirectory(serverpath);
}
string ext = file_upload.FileName.Substring(file_upload.FileName.LastIndexOf("."));
physical_file_name = filename + ext;
physical_file_path = serverpath + filename + ext;
file_upload.SaveAs(physical_file_path);

4、在用input file模拟时,还要记得在form的提交方式

<form id="form1" action="upload.aspx" method="post" enctype="multipart/form-data">

5、补充一下html头部的信息写法

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta name="author" content="tohours.com" />
        <title>后台管理登录</title>
    </head>
<body>
</body>
</html>

发表评论

邮箱地址不会被公开。