如何从应用程序下载到用户的系统中 [英] How to download from application into the user's system

查看:77
本文介绍了如何从应用程序下载到用户的系统中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望下载路径是Application用户系统的下载路径,现在是部署应用程序的服务器。



现在的问题是部署后的问题应用程序,同时单击导出按钮,它直接下载到服务器,而不是从我登录的计算机。



请帮助路径,以便它可以始终转到用户系统。



谢谢



我尝试过:



i want the download path to be that of the Application user system and now the server where the application is deployed.

the problem right now is that after deploying the application, while clicking on the export button it download straight to the server instead of the computer where im logging in from.

Kindly help with the path so it could always go to the user system.

thanks

What I have tried:

protected void BtnExport_Click1(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection(Cs))
        {
            conn.Open();
            if (ddlSearchGridview.SelectedValue == "-1")
            {
                lblMessage.Text = "Select A Status to Export";
                
            }
            else
            {
                try
                {
                    SqlDataAdapter sda = new SqlDataAdapter("GetDataToExportToCsv", conn);
                    sda.SelectCommand.CommandType = CommandType.StoredProcedure;
                    sda.SelectCommand.Parameters.AddWithValue("@complaintStatus", ddlSearchGridview.SelectedValue);

                    DataTable dt = new DataTable();
                    DataSet ds = new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter();
                    

                    //sda.Fill(ds);
                    sda.Fill(dt);

                    path = "C:\\excel\\CBN_CCMS_" + DateTime.Now.ToString("ddMMyyyy") + DateTime.Now.Millisecond + ".csv";
                   // CreateExcelFile.CreateExcelDocument(ds, path);
                    CreateCSVFile(dt, path);

                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
                finally
                {
                    conn.Close();
                }
            }
            
        }
    }

推荐答案

欢迎Emmablakes



你必须让用户选择一个文件保存位置和文件名。

解决方案如下:(抱歉,我有些傻瓜代码让你快速为我的机器工作)



Welcome Emmablakes

You will have to let the user choose a file save location and filename.
The solution is as follows: (sorry I dummied some code to get a quick one working on my machine for you)

using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Web.UI;

namespace WebFormsGridViewCommand
{
    public partial class _Default : Page
    {
        private string Cs = @"data source=(localdb)\mssqllocaldb;initial catalog=webformstest;integrated security=true";
        private string path;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnExport_Click(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(Cs))
            {
                conn.Open();
                //if (ddlSearchGridview.SelectedValue == "-1")
                //{
                //    lblMessage.Text = "Select A Status to Export";

                //}
                //else
                //{
                try
                {
                    SqlDataAdapter sda = new SqlDataAdapter("GetDataToExportToCsv", conn);
                    sda.SelectCommand.CommandType = CommandType.StoredProcedure;
                    //sda.SelectCommand.Parameters.AddWithValue("@complaintStatus", ddlSearchGridview.SelectedValue);

                    DataTable dt = new DataTable();
                    DataSet ds = new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter();


                    //sda.Fill(ds);
                    sda.Fill(dt);

                    path = "C:\\excel\\CBN_CCMS_" + DateTime.Now.ToString("ddMMyyyy") + DateTime.Now.Millisecond + ".csv";
                    // CreateExcelFile.CreateExcelDocument(ds, path);
                    CreateCSVFile(dt, path);

                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
                finally
                {
                    conn.Close();
                }
                //}

            }
        }

        private void CreateCSVFile(DataTable dt, string path)
        {
            var sb = new StringBuilder();

            // Add dt columns as csv first row headers:
            foreach (DataColumn dc in dt.Columns)
            {
                sb.Append(dc.ColumnName).Append(",");
            }
            sb.Remove(sb.Length - 1, 1);
            sb.AppendLine();

            // Add rows data to csv
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                foreach (DataColumn dc in dt.Columns)
                {
                    sb.Append(dt.Rows[i][dc.ColumnName]).Append(",");
                }
                sb.Remove(sb.Length - 1, 1);
                sb.AppendLine();
            }

            // Push csv string to server response for user to download
            string filename = Path.GetFileName(path); // Get the filename part only as the user will be asked where to save.

            Response.Clear(); // <- this will force the asp.net response to only deliver the csv file on button click, not html.

            // this tells the browser that it needs to prompt the user for a file save
            Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", filename));

            // This tells the browser the file type, in case it has built in viewer.
            Response.ContentType = "application/csv";

            // Sends the csv file content to the http response
            Response.Write(sb.ToString());
        }
    }
}


如果这是基于Web的服务器系统,则无法指定客户端destination:您可以按照显示的方式创建文件,但它将始终保存在服务器上,因为服务器代码无法直接访问客户端文件系统。

要将其发送到客户端,你需要使用Response.WriteFile(这里有一个例子: asp.net - ASP C#将文件发送到客户端 - 堆栈溢出 [ ^ ])并让客户决定放置它的位置。
If this is a web based server system, then you can't specify a client destination: you can create the file in the way you show, but it will always be saved on the server because Server code has no direct access to the Client file system.
To send it to the client, you would need to use Response.WriteFile (there is an example here: asp.net - ASP C# Send File to Client - Stack Overflow[^]) and let the client decide where to put it.


这篇关于如何从应用程序下载到用户的系统中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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