主题: ASP.net 上传图,完成后不知道如何做了
作者: crestonlang, 发布日期: 2010-04-30 16:34:08, 浏览数: 6379
image.js 提交到aspx页后,aspx页面处理如下,如何通知html页运行结果,以关掉上传窗
public partial class UpLoad : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int loop1;
        HttpFileCollection Files;
        Files = Request.Files;
        if(Files != null)
        {
            string[] arr1 = Files.AllKeys; 
            for (loop1 = 0; loop1 < arr1.Length; loop1++)
            {
                HttpPostedFile hpostfile = Files[loop1];
                string str = hpostfile.FileName;

                int EndStr = str.LastIndexOf("\\");
                str = str.Substring(EndStr);
                string filepath = Server.MapPath("upload");
                filepath += str;
                hpostfile.SaveAs(filepath);
            }
            string strWidth = Request["imgWidth"];
            string strHeight = Request["imgHeight"];
            string strAlign = Request["Align"];
            Response.Write("Fire in the hole~");
        }
    }
}
作者: crestonlang, 发布日期: 2010-05-05 15:30:44
暈,自己顶自己,从其他帖子中找到了方法,由于要配合其他XML接口,删除掉了部分功能,仅提供简单图片插入。
感谢einsta,是你的帖子提供了处理方法。
当然更感谢Roddy,你提供了这样一个优秀的功能。

public partial class UpLoad : System.Web.UI.Page
{
    private readonly string _UpLoadFolder = "upload";
    private string _relateFolder;

    protected void Page_Load(object sender, EventArgs e)
    {
            HttpPostedFile UploadFileHandle = Request.Files["imgFile"];
            string strWidth = Request["imgWidth"];
            string strHeight = Request["imgHeight"];
            string id = Request["id"];

            string myUrl = SaveFile(UploadFileHandle); 
            ReturnImg(id, myUrl, strWidth, strHeight);
    }

    private string RelateFolder
    {
        get { return _relateFolder; }
        set { _relateFolder = value; }
    }
    private string LocalFolder
    {
        get
        {
            string filePath;

            CUserCtrl pUserCtrl;
            pUserCtrl = (CUserCtrl)Session["UserCtrl"];
            RelateFolder = _UpLoadFolder + @"\" + pUserCtrl.UserName;

            filePath = Server.MapPath(RelateFolder);
            if(Directory.Exists(filePath) == false)
            {
                Directory.CreateDirectory(filePath);
            }

            return filePath;
        }
    }
    private string SaveFile(HttpPostedFile FileHandle)
    {
        string FileName = GetNewFileName();
        int extPos = FileHandle.FileName.LastIndexOf(".");
        string Ext = FileHandle.FileName.Substring(extPos);
        FileName += Ext;
        try
        {
            FileHandle.SaveAs(LocalFolder + "\\" + FileName);
        }
        catch(Exception e)
        {
            string msg = e.Message;
            Alert("Sorry,Upload file save fail.");
        }

        string url = RelateFolder;
        url = url.Replace(@"\", @"/");
        return url + "/" + FileName;
    }
    private string GetNewFileName()
    {
        DateTime time = DateTime.Now;
        Random RandGenerator = new Random();

        string newStr = RandGenerator.Next(0xffff).ToString("X");
        newStr += time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString();
        return newStr;
    }
    private void Alert(string MsgStr)
    {
        Response.Write("<html>");
        Response.Write("<head>");
        Response.Write("<title>error</title>");
        Response.Write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");
        Response.Write("</head>");
        Response.Write("<body>");
        Response.Write("<script type='text/javascript'>alert('" + MsgStr + "');</script>");
        Response.Write("</body>");
        Response.Write("</html>");
        Response.End();
    }


    private void ReturnImg(string id,string FileUrl, string FileWidth, string FileHeight)
    {
        Response.Write("<html>");
        Response.Write("<head>");
        Response.Write("<title>OK</title>");
        Response.Write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");
        Response.Write("</head>");
        Response.Write("<body>"); //id, url, title, width, height, border, align
        Response.Write("<script type=\"text/javascript\">parent.KE.plugin[\"image\"].insert('" + id + "','" + FileUrl + "','" + FileWidth + "','" + FileHeight + "');</script>");
        Response.Write("</body>");
        Response.Write("</html>");
        Response.End();
    }

}
回复
发表新帖 发表回复