我最近做一个webmail系统,其中收到eml格式的文件中间包含的有附件,打开eml文件下面的内容这样的
Content-Type: text/html; charset=gbk
Content-Transfer-Encoding: base64
PFRBQkxFIG NlbGxTcGFjaW5nPSIw IiBjZWxsUGFkZGluZz0iMCIgd2lkdGg9Ij k5JSI+CjxUQk9E
WT4KPFRSPgo 8VEQgd2lkdGg9IjY0IiBiY WNrZ3JvdW5kPSJodHRwOi8vbWlt Zy4xMjYubmV0L3Av
------=_Part_285175_602001323.1277188296156--       
      这段是附件文件,我一直没有搞明白是如何把这个附件能在点击页面的时候弹出对话框保存这个内容        public void SaveToPath(string path)
        {
            string fileName = this.FileName;
            if (string.IsNullOrEmpty(fileName)) fileName = Path.GetRandomFileName();            this.SaveToFile(Path.Combine(path, fileName));
        }
这个函数可以把二进制文件提取保存到制定的目录下,但是保存到服务器上面去了,我希望能客户点的时候能弹出一个对话框,提示用户保存文件,类似163邮箱里面的附件功能。 希望有高手能指点一下谢谢

解决方案 »

  1.   

    我觉得可以添加一个按钮,当用户点击的时候提示用户,选择本地路径,传递本地路径给你的
    SaveToPath(string path),这样应该可以吧?
      

  2.   

    二进制文件如果想换为了一个文件,必须首先知道它的二进制是怎么转换的。
    拿word为例:是将word整个文件转换了二进制数据,还是将word内容转换了二进制数据。
    附件如果是图片文件,那就没的说了。将整个文件转换了二进制数据。
      

  3.   

    word文件与二进制的转换:方法1: /// <summary>
        /// 二进制数据转换为word文件
        /// </summary>
        /// <param name="data">二进制数据</param>
        /// <param name="fileName">word文件名</param>
        public string ConvertWord(byte[] data, string fileName)
        {
            string savePath = @"\SystemWord\"; 
            if (!System.IO.Directory.Exists(GetPath() + savePath))
            {
                Directory.CreateDirectory(GetPath() + savePath);
            }
            savePath += fileName + ".doc";
            string filePath = GetPath() + savePath;
            
            FileStream fs;
            if (System.IO.File.Exists(filePath))
            {
                fs = new FileStream(filePath, FileMode.Truncate);
            }
            else
            {
                fs = new FileStream(filePath, FileMode.CreateNew);
            }
            BinaryWriter br = new BinaryWriter(fs);
            br.Write(data, 0, data.Length);
            br.Close();
            fs.Close();
            return savePath;
        }    /// <summary>
        /// word文件转换二进制
        /// </summary>
        /// <param name="wordPath">word文件路径</param>
        /// <returns>二进制</returns>
        private byte[] wordConvertByte(string wordPath)
        {
            byte[] bytContent = null;
            System.IO.FileStream fs = null;
            System.IO.BinaryReader br = null;
            try
            {
                fs = new FileStream(wordPath, System.IO.FileMode.Open);
            }
            catch
            {
            }
            br = new BinaryReader((Stream)fs);
            bytContent = br.ReadBytes((Int32)fs.Length);        return bytContent;
        }
    方法2:-------------将文件转换为二进制,并保存到数据库中----------------            string strResult = strPath + @"\result.doc";            System.IO.FileStream fs = new System.IO.FileStream(@strResult, System.IO.FileMode.Open);
                fs.Position = 0;
                byte[] content = new byte[fs.Length];
                fs.Read(content, 0, (int)fs.Length);
                fs.Close();
                fluidDesignDoc.qhse1 = content;//其中qhse1在数据库中保存的类型为OLE对象
                IFluidDesignDocManager manager = new FluidDesignDocManager();
                manager.UpdateFluidDesignDoc(fluidDesignDoc);
    -------------将二进制数据转化为指定位置文件---------------------private void getFile(byte[] content, string filePath)
            {
                string fileName = filePath;            if (System.IO.File.Exists(fileName))
                {
                    System.IO.File.Delete(fileName);
                }            //FileStream sw = new FileStream(@fileName, FileMode.Create);
                //StreamWriter fs = new StreamWriter(sw, Encoding.UTF8);
                //fs.Write(entity.Content);
                System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Create);
                fs.Write(content, 0, content.Length);
                fs.Flush();
                fs.Close();
                fileName = System.IO.Path.Combine(Application.StartupPath, fileName);
            }
      

  4.   

    /// <summary>
        /// 二进制数据转换为word文件
        /// </summary>
        /// <param name="data">二进制数据</param>
        /// <param name="fileName">word文件名</param>
        public string ConvertWord(byte[] data, string fileName)
        {
            string savePath = @"\SystemWord\"; 
            if (!System.IO.Directory.Exists(GetPath() + savePath))
            {
                Directory.CreateDirectory(GetPath() + savePath);
            }
            savePath += fileName + ".doc";
            string filePath = GetPath() + savePath;
            
            FileStream fs;
            if (System.IO.File.Exists(filePath))
            {
                fs = new FileStream(filePath, FileMode.Truncate);
            }
            else
            {
                fs = new FileStream(filePath, FileMode.CreateNew);
            }
            BinaryWriter br = new BinaryWriter(fs);
            br.Write(data, 0, data.Length);
            br.Close();
            fs.Close();
            return savePath;
        }    /// <summary>
        /// word文件转换二进制
        /// </summary>
        /// <param name="wordPath">word文件路径</param>
        /// <returns>二进制</returns>
        private byte[] wordConvertByte(string wordPath)
        {
            byte[] bytContent = null;
            System.IO.FileStream fs = null;
            System.IO.BinaryReader br = null;
            try
            {
                fs = new FileStream(wordPath, System.IO.FileMode.Open);
            }
            catch
            {
            }
            br = new BinaryReader((Stream)fs);
            bytContent = br.ReadBytes((Int32)fs.Length);        return bytContent;
        }
      

  5.   

    jaylianyu你发的-------------将二进制数据转化为指定位置文件--------------------- 这个很有用,但是如何把这个文件保存到客户端的路径,而不是在一个服务器上的路径呢
      

  6.   

    楼上的朋友能否详细的说一下如何用流操作我刚才又查了一下资料,说是把这个二进制文件用filestream写入到http让客户端下载,但是这个具体过程不知道如何写的,麻烦给一个详细的写法,非常感谢
      

  7.   


    客户端访问当然是客户端路径了。
    private void getFile(byte[] content, string filePath)整个方法传的就是二进制数据与客户端想保持的路径。
      

  8.   


    我这个是web格式的,客户端访问是通过http页面访问的,这个aspx页面如何弹出文件保存的路径呢?