Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET转换文件存储在服务器驱动器中,而不是用户PC中_C#_Asp.net_Visual Studio 2005 - Fatal编程技术网

C# ASP.NET转换文件存储在服务器驱动器中,而不是用户PC中

C# ASP.NET转换文件存储在服务器驱动器中,而不是用户PC中,c#,asp.net,visual-studio-2005,C#,Asp.net,Visual Studio 2005,我正在使用VS2005 c# 我有这个功能将管道分隔的文本文件转换为excel格式 然后,这些文件将存储在用户PC的C:中 但是,当我将web应用程序部署到测试服务器中时,文件会保存在测试服务器的C:\中 下面是我的代码片段,我可以知道如何将其更改为保存在用户的pc而不是服务器上,或者如果可能,显示一条弹出消息供用户打开/保存文件。多谢各位 if (TextFile.HasFile) { string strFileName = Server.HtmlEncode(Tex

我正在使用VS2005 c#

我有这个功能将管道分隔的文本文件转换为excel格式

然后,这些文件将存储在用户PC的C:

但是,当我将web应用程序部署到测试服务器中时,文件会保存在测试服务器的C:\中

下面是我的代码片段,我可以知道如何将其更改为保存在用户的pc而不是服务器上,或者如果可能,显示一条弹出消息供用户打开/保存文件。多谢各位

if (TextFile.HasFile)
    {
        string strFileName = Server.HtmlEncode(TextFile.FileName);

        string strExtension = Path.GetExtension(strFileName);

            string activeDir = @"C:\";

            string newPath = System.IO.Path.Combine(activeDir, "App_Converted");

            System.IO.Directory.CreateDirectory(newPath);

            string strExcelOutputFilename = "C:/App_Converted/" + xlExtension;

            using (StreamWriter outputWriter = new StreamWriter(File.Create(strExcelOutputFilename)))
            {
                StreamReader inputReader = new StreamReader(TextFile.FileContent);
                while (inputReader.Peek() >= 0)
                {
                    string[] myInputFields = inputReader.ReadLine().Split(new char[] { '|' });
                    List<string> myOutputFields = new List<string>();

                    foreach (string aField in myInputFields)
                    {
                        string oField = aField;
                        if (oField.Contains(","))
                            oField = "\"" + oField + "\"";

                            myOutputFields.Add(oField);
                    }

                    outputWriter.WriteLine(string.Join(",", myOutputFields.ToArray()));

                }
                inputReader.Close();
                UploadStatusLabel.Text = "File stored at [C:/App_Converted/] as file name [namelist.csv]";
                Response.Clear();
                Response.ContentType = "application/vnd.xls";
                Response.AddHeader("Content-Disposition", "attachment;filename=namelist.csv");

                StringWriter swriter = new StringWriter();
                HtmlTextWriter hwriter = new HtmlTextWriter(swriter);

                Response.Write(swriter.ToString());
                Response.End();
                return;
            }
if(TextFile.HasFile)
{
字符串strFileName=Server.HtmlEncode(TextFile.FileName);
字符串strExtension=Path.GetExtension(strFileName);
字符串activeDir=@“C:\”;
字符串newPath=System.IO.Path.Combine(activeDir,“App_转换”);
System.IO.Directory.CreateDirectory(newPath);
字符串strExcelOutputFilename=“C:/App_Converted/”+xlExtension;
使用(StreamWriter outputWriter=new StreamWriter(File.Create(strExcelOutputFilename)))
{
StreamReader inputReader=新的StreamReader(TextFile.FileContent);
while(inputReader.Peek()>=0)
{
字符串[]myInputFields=inputReader.ReadLine().Split(新字符[]{'.});
List myOutputFields=新列表();
foreach(myInputFields中的字符串字段)
{
场串=远场;
如果(字段包含(“,”))
oField=“\”+oField+“\”;
myOutputFields.Add(oField);
}
outputWriter.WriteLine(string.Join(“,”,myOutputFields.ToArray());
}
inputReader.Close();
UploadStatusLabel.Text=“存储在[C:/App_Converted/]的文件名为[namelist.csv]”;
Response.Clear();
Response.ContentType=“application/vnd.xls”;
AddHeader(“内容处置”、“附件;文件名=namelist.csv”);
StringWriter swriter=新StringWriter();
hwriter=新的HtmlTextWriter(swriter);
Write(swriter.ToString());
Response.End();
返回;
}

编辑:


使用保存窗口更新了代码。但是,我不知道如何将转换后的文件链接到保存窗口。此处需要一些帮助。以上是我的更新代码。

您无法使用上述方法将文件保存到客户端计算机。 您可以像在这里一样将其存储在服务器中,并提供指向客户端的(href)链接。 或者,您可以将该文件作为响应写入浏览器,浏览器将打开该文件或提供保存选项。请使用

,而不是:

    Response.ContentType = "application/vnd.xls";
    Response.AddHeader("Content-Disposition", "attachment;filename=namelist.csv");

    StringWriter swriter = new StringWriter();
    HtmlTextWriter hwriter = new HtmlTextWriter(swriter);

    Response.Write(swriter.ToString());
使用这个片段

    Response.ContentType = "application/vnd.xls";
    Response.AddHeader("Content-Disposition", "attachment;filename=namelist.csv");
    string filePath = @"paste a path of your file here";
    Response.WriteFile(filePath);
p、 美国。
请阅读我在评论中发布的so问题。它准确地回答了您的问题,并涵盖了如何强制从服务器下载文件的许多方面

您不能简单地将文件保存在用户的C驱动器中。代码在服务器中执行。文件保存在服务器中。可能重复。请检查此so问题并仔细考虑有一分钟-上面的ASP.NET代码在服务器上运行,因此它存储在服务器驱动器上是有意义的。如果这是一个intranet应用程序,并且您在LAN上,您可以为您的应用程序池提供提升的域凭据(不可取),并使用UNC路径(\\RemotePC\c$\)但是,若要通过internet将其保存到用户的PC,您可能需要要求用户下载文件并将其存储在建议的位置。我想更改代码以打开弹出消息,就像在internet Explorer中典型的下载一样。我可以知道如何做吗?我已经阅读了该页面,我已经更新了代码。需要一些帮助吗对于转换文件的输出,必须设置Response.ContentType