日期:2014-05-17  浏览次数:20452 次

一个小问题,新手实在弄不出来,代码很清晰简单。
以下是html代码
HTML code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>11</title>
    <script type="text/javascript" language="javascript">
    function getif(txt1)
    {
        var txt = document.getElementById("txt_1");
        txt.value = txt1;
    }
    </script>
</head>
<body>
    <iframe style="display:block;" name="hideframe" id="testframe"></iframe>    
    <div id = "nofri">  
        <form id = "imageform" method ="post" action= "asp/upload.ashx" enctype ="multipart/form-data" target= "hideframe">
            <input type="submit" value="上传"/>
            <input type="file" name="file" />
            <input type="text" id="txt_1" />
            <input type="button" id="btn_1" onclick = "getif()" value = "取值"/>
        </form>  
        
    </div>
</body>
</html>


以下是提交到得一般处理程序,用C#编写
C# code

<%@ WebHandler Language="C#" Class="upload" %>

using System;
using System.Web;
using System.IO;
using System.Net;
public class upload : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";
        context.Response.ContentType = "utf-8";
        HttpPostedFile file = context.Request.Files["file"];
        string newFilename = Guid.NewGuid().ToString();
        var extension = Path.GetExtension(file.FileName).ToUpper();
        newFilename += extension;
        string filepath = string.Empty;
        if (SaveAsImg(newFilename, file, context))
        {
            //context.Response.Write("<div id='tframe'>" + newFilename +  "</div>");  
            context.Response.Write("<script>parent.getif('" + newFilename + "')</script>");  
        }
        else
        {
            context.Response.Write("false");
        }
        
    }

    public bool SaveAsImg(string filename, HttpPostedFile file, HttpContext context)
    {
        HttpServerUtility server = context.Server;
        try
        {
            string filepath = server.MapPath("~/img/userimg/" + filename);
            file.SaveAs(filepath);
            return true;
        }
        catch
        {
            return false;
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}


问题是,在chrome、FF等内核的浏览器下,该段代码都能达到预期效果。从upload.ashx中取得文件名。
但ie下,iframe直接将返回的代码显示<script>parent.getif('" + newFilename + "')</script>

------解决方案--------------------
探讨
2楼,在chrome和ff中还是正常的,但是ie下,加什么都是直接显示在iframe中。

3楼,改了这句也一样。