<input type=file id=fileName runat="server">的postedfile.filename值为某个“路径\路径\路径\文件.jpg”演示一:
string strFullName=fileName.PostedFile.FileName;
Response.Write(strFullName);
Response.End();
结果是:页面显示  "路径\路径\路径\文件.jpg"  --(正确,是我需要的结果)
演示二:
string strFullName=fileName.PostedFile.FileName;
Response.Write("<script language=javascript>alert('"+strFullName+"');</script>");
Response.End();
结果是:弹出窗口显示  "路径路径路径文件.jpg"  --(错误!“\”被莫名其妙的喀嚓掉了,)------------------------------------
我想破了头也没搞明白是怎么回事!
哪位大哥知道这是为什么啊??

解决方案 »

  1.   

    Response.Write("<script language=javascript>alert(')";
    Response.Write("strFullName");
    Response.Write("')</script>");
    我觉得应该要这样写。
      

  2.   

    这么晚还没睡呀,呵呵,跟我一样夜猫子^^你的方法我用过的,不行的,所以我非常奇怪,难道这是一个bug??
    难道含有"\"的字符在<input type=text>和alert()里不能使用??
    不应该呀~~~
      

  3.   

    我基本上是白天睡觉晚上学习和做事情。过的是黑白颠倒的生活。
    如果不行的话试试看这样行不行?先把值写进一个文本框?然后再把它用'"+ 文本框.Text.Trim() +"'然后alert()出来?
      

  4.   

    我用alert()来测试这个值的目的,就是为了要把这个值放到一个文本框里,结果是文本框得到的值是“路径路径路径文件名.jpg”,你说我郁闷不郁闷...
      

  5.   

    既然是要用文本框得到值的话,那就直接把变量的值赋给文本框了,还要alert干嘛?
    要测试输出的话直接用Response.Write("")啦。
    alert我也感觉用起来挺麻烦。
    不知道MessageBox是用哪个名字空间。要是有这个东西的话那就省事很多了。
      

  6.   

    是这样的,我有一个图片上传的文件,上传页面因为需要,而使用了showModalDialog(),我在这个子窗口里完成操作后,需要把文件路径和名称传给父窗口里的<input type=text id=urlFile>.
    但是问题出来了,值是传过去了,但是“\”却被喀嚓掉了。。所以我用alert在子窗口里进行测试,看看strFullName出了什么问题...代码如下:
    -----------------------------------------------------
    string strFullName=fileName.PostedFile.FileName;if(strFileType!="swf"){
       Response.Write("<script language=javascript>alert('Flash文件格式错误,正确格式为Swf !');window.location.href=window.location.href;</script>");
    }else{
      string IsOk=UpLoadFlash(); //flash文件上传函数,返回一个是否成功的字符串值
      if(IsOk=="ok"){
        Response.Write("<script language=javascript>parent.document.form1.urlFile.value='"+strFullName.Trim()+"';</script>");  //错误问题出在这里,我的父窗口的urlFile的值被喀嚓掉"\"了
        
      }
    }
    }
      

  7.   

    我连图片上传都还没研究完呢,今晚刚好在研究怎么生成缩略图。
    你用try{}catch{}finally{}语句结构改写你上面的代码试试看可不可以,具体的我也不会。
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Drawing.Imaging;
    namespace Exam_C
    {
    /// <summary>
    /// ToThumbnailImage 的摘要说明。
    /// </summary>
    public class ToThumbnailImage : System.Web.UI.Page
    {
    /*  
    Create By lion  
    2003-05-20 19:00  
    Copyright (C) 2004 www.LionSky.Net. All rights reserved. 
    Web: http://www.Lionsky.net ;
    Email: [email protected] 
    */  
    static Hashtable htmimes=new Hashtable();
    internal readonly string AllowExt = ".jpe|.jpeg|.jpg|.png|.tif|.tiff|.bmp";

    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    #region htmimes[".jpe"]="image/jpeg";
    htmimes[".jpeg"]="image/jpeg";
    htmimes[".jpg"]="image/jpeg";
    htmimes[".png"]="image/png";
    htmimes[".tif"]="image/tiff";
    htmimes[".tiff"]="image/tiff";
    htmimes[".bmp"]="image/bmp";
    #endregion
    //调用生成缩略图方法
    ToThumbnailImages("lionsky.jpg","b.gif",300);
    }
    #endregion #region Helper

    /// <summary>
    /// 获取图像编码解码器的所有相关信息
    /// </summary>
    /// <param name="mimeType">包含编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串</param>
    /// <returns>返回图像编码解码器的所有相关信息</returns>
    static ImageCodecInfo GetCodecInfo(string mimeType)
    {
    ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
    foreach(ImageCodecInfo ici in CodecInfo)
    {
    if(ici.MimeType == mimeType)return ici;
    }
    return null;
    } /// <summary>
    /// 检测扩展名的有效性
    /// </summary>
    /// <param name="sExt">文件名扩展名</param>
    /// <returns>如果扩展名有效,返回true,否则返回false.</returns>
    bool CheckValidExt(string sExt)
    {
    bool flag=false;
    string[] aExt = AllowExt.Split('|');
    foreach(string filetype in aExt)
    {
    if(filetype.ToLower()==sExt)
    {
    flag = true;
    break;
    }
    }
    return flag;
    } /// <summary>
    /// 保存图片
    /// </summary>
    /// <param name="image">Image 对象</param>
    /// <param name="savePath">保存路径</param>
    /// <param name="ici">指定格式的编解码参数</param>
    void SaveImage(System.Drawing.Image image,string savePath,ImageCodecInfo ici)
    {
    //设置 原图片 对象的 EncoderParameters 对象
    EncoderParameters parameters = new EncoderParameters(1);
    parameters.Param[0] = new EncoderParameter(Encoder.Quality, ((long) 90));
    image.Save(savePath, ici, parameters);
    parameters.Dispose();
    }
    #endregion #region Methods /// <summary>
    /// 生成缩略图
    /// </summary>
    /// <param name="sourceImagePath">原图片路径(相对路径)</param>
    /// <param name="thumbnailImagePath">生成的缩略图路径,如果为空则保存为原图片路径(相对路径)</param>
    /// <param name="thumbnailImageWidth">缩略图的宽度(高度与按源图片比例自动生成)</param>
    public void ToThumbnailImages(string sourceImagePath,string thumbnailImagePath,int thumbnailImageWidth)
    {
    string SourceImagePath = sourceImagePath;
    string ThumbnailImagePath = thumbnailImagePath;
    int ThumbnailImageWidth = thumbnailImageWidth;
    string sExt = SourceImagePath.Substring(SourceImagePath.LastIndexOf(".")).ToLower();
    if(SourceImagePath.ToString()==System.String.Empty) throw new NullReferenceException("SourceImagePath is null!");
    if(!CheckValidExt(sExt))
    {
    throw new ArgumentException("原图片文件格式不正确,支持的格式有[ "+ AllowExt +" ]","SourceImagePath");
    }
    //从 原图片 创建 Image 对象
    System.Drawing.Image image = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(SourceImagePath));
    int num = ((ThumbnailImageWidth / 4) * 3);
    int width = image.Width;
    int height = image.Height;
    //计算图片的比例
    if ((((double) width) / ((double) height)) >= 1.3333333333333333f)
    {
    num = ((height * ThumbnailImageWidth) / width);
    }
    else
    {
    ThumbnailImageWidth = ((width * num) / height);
    }
    if ((ThumbnailImageWidth < 1) || (num < 1))
    {
    return;
    }
    //用指定的大小和格式初始化 Bitmap 类的新实例
    Bitmap bitmap = new Bitmap(ThumbnailImageWidth, num, PixelFormat.Format32bppArgb);
    //从指定的 Image 对象创建新 Graphics 对象
    Graphics graphics = Graphics.FromImage(bitmap);
    //清除整个绘图面并以透明背景色填充
    graphics.Clear(Color.Transparent);
    //在指定位置并且按指定大小绘制 原图片 对象
    graphics.DrawImage(image, new Rectangle(0, 0, ThumbnailImageWidth, num));
    image.Dispose();
    try
    {
    //将此 原图片 以指定格式并用指定的编解码参数保存到指定文件
    string savepath = (ThumbnailImagePath==null?SourceImagePath:ThumbnailImagePath);
    SaveImage(bitmap,HttpContext.Current.Server.MapPath(savepath),GetCodecInfo((string)htmimes[sExt]));
    }
    catch(System.Exception e)
    {
    throw e;
    }
    finally
    {
    bitmap.Dispose();
    graphics.Dispose();
    }
    }
    #endregion }
    }
      

  8.   

    Kflash兄:早点休息吧,问题被我解决了
    我加了一行代码,结果OK了string strFullName=fileName.PostedFile.FileName;
    strFullName=strFullName.Replace("\\","\\\\");