在Asp.net上下载FIles

时间:2016-04-04 04:49:12

标签: c# sql asp.net

这是我的代码

protected void btnUpload_Click(object sender, EventArgs e)
{
    try
    {
        string filename = Path.GetFileName(FileUpload1.FileName);
        FileUpload1.SaveAs(Server.MapPath("~/CustomerFiles/") + filename);
        lblResult.Text = "Your File " + filename + " is Succesfull uploaded!";
        //lblFile.Text = FileUpload1.FileName;
        lblFile.Text = "../CustomerFiles/" + filename;
        Page_Load(sender, e);
    }

    catch (Exception)

    {
        lblResult.Text = "Upload Failed";
    }
}

我可以上传文件,但我无法在我的visual studio 2013中看到它。我需要查看我上传的文件,以便我下载并将文件路径保存到Sql数据库。

1 个答案:

答案 0 :(得分:0)

这是我下载文件的代码

protected void Button1_Click(object sender, EventArgs e)
    {
        string filename = "~/Files/Mastering MeteorJS Application Development.pdf";
        String _fileName = "akash";
        string path = MapPath(filename);
        //byte[] bts = System.IO.File.ReadAllBytes(path);
        Response.Buffer = true;
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition:", "attachment;filename=" + _fileName + ".pdf");
        Response.TransmitFile(path);
        //Response.Close();
    }

发布Web应用程序时遇到问题。在将应用程序发布到文件系统后,此Files文件夹不会复制到visual studio 2010上的已发布文件夹中。因此我手动复制Files发布Web应用程序并粘贴到该目录中的内容目录。现在好了