直接保存文件 [英] saving a file directly

查看:65
本文介绍了直接保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想直接点击链接下载并保存文件...请帮助

解决方案

如果用户无法专门保存文件点击链接:浏览器将遵守用户设置的任何规则,包括任何需要确认的规则。



如果您的意思是我如何给用户点击链接,然后下载文件?然后它取决于许多因素:

1)文件是否在可用文件夹中相对于网站?如果是这样,那么你可以给文件一行:

 <   a     href   =  http://MyDomain.com/folder/file.txt > 点击此处下载!<   / a  >  



2)文件是否存储在数据库中?如果是,则提供类似于上述的链接,但通过ASPX页面引用文件:

 <   a     href   =  wm5ftdl.aspx?file = 2552129f-4629-4432-9de3-8177870d81be    target   =  _ blank  > 点击此处下载!<   / a  >  

然后在页面中,从数据库中检索数据:

 <%@       语言  =  C#    AutoEventWireup   =  true    CodeFile   =  wm5ftdl.aspx.cs   继承  =  wm5ftdl   %>  

<%
// 在给定文件名的情况下将下载文件发送到客户端。
string guid = Request.QueryString [ 文件];
string fileName = ERROR;
byte [] data = new byte [] { 0 0 0 0 };
string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings [ DownloadDatabase]的ConnectionString。
使用(System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient。 SqlConnection(strCon))
{
con.Open();
string strcmd = SELECT [iD] ,cn。[fileName],[description],[dataContent],[version] +
FROM dlContent cn +

WHERE cn.iD = @ID;
使用(System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient。 SqlCommand(strcmd,con))
{
cmd.Parameters.AddWithValue( @ ID ,guid);
使用(System.Data.SqlClient.SqlDataReader r = cmd.ExecuteReader())
{
if (r.Read())
{
fileName =( string )r [ filename];
data =( byte [])r [ dataContent];
}
}
}
}
Response.Clear();
Response.AddHeader( Cache-Control no-cache,must-revalidate,post-check = 0,pre-check = 0);
Response.AddHeader( Pragma no-cache);
Response.AddHeader( Content-Description 文件下载);
Response.AddHeader( Content-Type application / force-download);
Response.AddHeader( Content-Transfer-Encoding binary \ n);
Response.AddHeader( content-disposition attachment; filename = + fileName);
Response.BinaryWrite(data);
// Response.WriteFile(wm5ftdata /+ fileName);
到Response.End();
%>



.CS文件中没有任何内容:

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;

public partial class wm5ftdl:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{

}
}


I want to directly download and save a file on click of a link...please help

解决方案

You can't specifically save the file if the user clicks on a link: the browser will obey whatever rules the user has set up, including any which require a confirmation.

If you mean "how do I give a user a link to click on, and it downloads a file?" then it depends on a number of factors:
1) Is the file in an available folder relative to the website? If so, then you can just give a line to the file:

<a href="http://MyDomain.com/folder/file.txt">Click here to download!</a>


2) Is the file stored in a database? If so, then give a link similar to the above, but with a file reference via an ASPX page:

<a href="wm5ftdl.aspx?file=2552129f-4629-4432-9de3-8177870d81be" target="_blank">Click here to download!</a>

Then in the page, retrieve the data from the DB:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="wm5ftdl.aspx.cs" Inherits="wm5ftdl" %>

<%  
    // Send a download file to the client given the filename.    
    string guid = Request.QueryString["file"];
    string fileName = "ERROR";
    byte[] data = new byte[] { 0, 0, 0, 0 };
    string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DownloadDatabase"].ConnectionString;
    using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon))
        {
        con.Open();
        string strcmd = "SELECT [iD] ,cn.[fileName],[description] ,[dataContent] ,[version] " +
                        "FROM dlContent cn " +

                        "WHERE cn.iD=@ID";
        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(strcmd, con))
            {
            cmd.Parameters.AddWithValue("@ID", guid);
            using (System.Data.SqlClient.SqlDataReader r = cmd.ExecuteReader())
                {
                if (r.Read())
                    {
                    fileName = (string) r["filename"];
                    data = (byte[]) r["dataContent"];
                    }
                }
            }
        }
    Response.Clear();
    Response.AddHeader("Cache-Control", "no-cache, must-revalidate, post-check=0, pre-check=0");
    Response.AddHeader("Pragma", "no-cache");
    Response.AddHeader("Content-Description", "File Download");
    Response.AddHeader("Content-Type", "application/force-download");
    Response.AddHeader("Content-Transfer-Encoding", "binary\n");
    Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
    Response.BinaryWrite(data);
    //Response.WriteFile("wm5ftdata/" + fileName);
    Response.End();  
%>


There is nothing much in the .CS file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class wm5ftdl : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
        {

        }
    }


这篇关于直接保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆