以下是html代码<!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#编写<%@ 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>

解决方案 »

  1.   

    加编码试试,名字可能有中文导致乱码什么的了context.Response.Write("<script type='text/javascript' charset='utf-8'>parent.getif('" + newFilename + "')</script>");   
      

  2.   

    context.Response.ContentType = "utf-8";
    =====================>
    context.Response.ContentEncoding = System.Text.Encoding.UTF8;
      

  3.   

    2楼,在chrome和ff中还是正常的,但是ie下,加什么都是直接显示在iframe中。3楼,改了这句也一样。
      

  4.   

    怪了啊,我的ie就不行,我很郁闷这事,怎么样都是直接显示代码在iframe中。chrome和ff都跟5楼一样。
      

  5.   

    context.Response.ContentType = "text/html"; 这句去掉