使用asp.net / c#.net将excel文件从服务器复制或移动到客户端计算机 [英] copy or move excel file from server to client machine using asp.net / c#.net

查看:76
本文介绍了使用asp.net / c#.net将excel文件从服务器复制或移动到客户端计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..





i来复制或移动指定的excel文件C:\\ myfile.xls服务器机器到客户端机器使用asp.net网站我的网站托管服务器iis所以任何代码或建议这样做,如果有任何想法请帮助我。





谢谢。

hi ..


i wan to copy or move specified excel file "C:\\myfile.xls" from server machine to client machine using asp.net website my web site was hosted on server iis so any code or suggestion for do this if any idea about this please help me.


Thank you.

推荐答案

可能你不能:你网站运行的用户的默认权限几乎肯定会不允许您访问服务器根目录中的文件,并且您的网站无法在客户端上指定文件的存储位置(或者即使它完全存储 - 这取决于用户)



但是......这是我用来执行相同任务的代码:

Probably, you can't: The default permissions for the user your website is running under will almost certainly not allow you to access files in the root directory of your server, and your website cannot specify the storage location of a file on the client (or even if it is stored at all - that is up to the user)

But...this is the code I use to perform much teh same task:
<%@ 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();  
%>

它从数据库中检索文件数据,但这很容易更改。

It retrieves the file data from a DB, but that's easy to change.


这篇关于使用asp.net / c#.net将excel文件从服务器复制或移动到客户端计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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