Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
Kriti Jain 20-Nov-13 4:46am    
Do you want to download the file from server to client machine?
bhavesh002 20-Nov-13 5:21am    
Yes

1 solution

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:
ASP.NET
<%@ 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.
 
Share this answer
 
Comments
bhavesh002 20-Nov-13 5:24am    
i was tried your sample code i was retrieves the file data from my file but this work on my app but when same code i run on server it was not work.
OriginalGriff 20-Nov-13 5:31am    
Show the exact code you tested on the server.
bhavesh002 20-Nov-13 5:32am    
byte[] bytes = System.IO.File.ReadAllBytes("C:\\csharp_Excel.xls");


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=" + "C:\\csharp_Excel.xls");
Response.BinaryWrite(bytes);
HttpContext.Current.ApplicationInstance.CompleteRequest();
bhavesh002 20-Nov-13 5:35am    
this work when i was trying in sample application on my local pc but when i was put the same on server app it not give any popup and also not save file and one more thing it not give any error msg or exception code debug successfully
OriginalGriff 20-Nov-13 5:52am    
As I said:

"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"

Move the file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900