注册 登录
编程论坛 ASP.NET技术论坛

下载问题

hadisi0109 发布于 2008-03-05 10:30, 592 次点击
我想做一个下载功能但不知道怎么做?我直接给加连接到下载的路径,但是没有弹出下载的对话框,而是显示无法找到网页.请各位指点一下应该怎么做?最好有代码
3 回复
#2
jalonlovesja2008-03-05 11:26
if (FileUpload1.PostedFile != null)
       {   
            string nam = FileUpload1.PostedFile.FileName ;
            //取得文件名(抱括路径)里最后一个"."的索引
            int i= nam.LastIndexOf(".");
            //取得文件扩展名
            string newext =nam.Substring(i);
            //这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
            DateTime now = DateTime.Now;
            string newname = now.DayOfYear.ToString() + FileUpload1.PostedFile.ContentLength.ToString();
            //保存文件到你所要的目录,这里是IIS根目录下的upload目录.你可以改变.
            //注意: 我这里用Server.MapPath()取当前文件的绝对目录.在里""必须用""代替
            FileUpload1.PostedFile.SaveAs(Server.MapPath("upload\\") + newname + newext);
            this.HyperLink1.NavigateUrl  =("upload\\")+newname+newext;
       }
#3
hadisi01092008-03-05 16:08
下载问题
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using
using System.Data.SqlClient;


public partial class download : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        string sql = "select Accessory from mail where ID = +id";
        SqlConnection conn = Database.GetOpenDatabase();
        SqlCommand cmd = new SqlCommand(sql,conn);
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        da.SelectCommand = cmd;
        da.Fill(ds);
        if(ds.Tables[0].Rows.Count>0)
        {
            //string path = ds.Tables[0].Rows[0]["Accessory"].ToString();
            string path = "//download//jianli.doc";
            if (path != null)
            {
                FileDownloadByFullName(path);
            }
        }
    }
    public static void FileDownloadByFullName(string FullFileName)
    {
        FileInfo DownloadFile = new FileInfo(FullFileName);
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.Buffer = false;
        HttpContext.Current.Response.ContentType = "application/octet-stream";
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.ASCII));
        HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
        HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
    }  
}
但是老出现找不到路径,请帮忙看看
#4
jalonlovesja2008-03-06 14:02
把"//"改为"\\"了,我估计是你写反了
1