Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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# 将文件下载到文件夹,而不是下载文件夹.net core_C#_Asp.net Core - Fatal编程技术网

C# 将文件下载到文件夹,而不是下载文件夹.net core

C# 将文件下载到文件夹,而不是下载文件夹.net core,c#,asp.net-core,C#,Asp.net Core,我有以下方法,当从窗体操作链接单击时,将在新的浏览器窗口中自动打开PDF和图像。如何将所有其他文件下载到电脑上的以下路径。C:\Temp [HttpGet] public async Task<IActionResult> Download(int DocumentRepositoryID) { var documents = await CustomerFleetManagementService.ViewDocumentAsync(Docu

我有以下方法,当从窗体操作链接单击时,将在新的浏览器窗口中自动打开PDF和图像。如何将所有其他文件下载到电脑上的以下路径。C:\Temp

    [HttpGet]
    public async Task<IActionResult> Download(int DocumentRepositoryID)
    {
        var documents = await CustomerFleetManagementService.ViewDocumentAsync(DocumentRepositoryID);
        return File(documents.ImgData, documents.ImgContentType);
    }
[HttpGet]
公共异步任务下载(int-DocumentRepositoryID)
{
var documents=wait CustomerFleetManagementService.ViewDocumentAsync(DocumentRepositoryID);
返回文件(documents.ImgData,documents.ImgContentType);
}

以下代码从API获取一个文档,然后在客户端C:中创建一个路径,其中包含一个名为Temp2的文件夹

然后,FileStream创建一个文件,该文件具有您想要的任何文件名。例如documents.ImgName=“ABC.docx”

然后打开一个新的流,其中我传递一个字节数组,零(0)作为默认索引和文件的长度

 [HttpGet]
    public async Task<IActionResult> Download(int DocumentRepositoryID, int customerID)
    {
        var documents = await CustomerFleetManagementService.ViewDocumentAsync(DocumentRepositoryID);
        
        string path = @"C:\Temp2";
        System.IO.Directory.CreateDirectory(path);
        FileStream stream = new FileStream(Path.Combine(path, documents.ImgName), FileMode.Create);
        stream.Write(documents.ImgData, 0, documents.ImgLength);
        stream.Close();
        new ProcessStartInfo(@"C:\Temp2\"+documents.ImgName).StartProcess();
        return RedirectToAction("Files", "Customer", new { customerID = customerID });
    }

  public static class UT
    {
            public static Process StartProcess(this 
            ProcessStartInfoo psi, bool useShellExecute = 
            true)
    {
        psi.UseShellExecute = useShellExecute;
        return Process.Start(psi);
    }
    }
ProcessStartInfo然后从Temp2文件夹自动打开文件,而最终用户无需手动打开文件

 [HttpGet]
    public async Task<IActionResult> Download(int DocumentRepositoryID, int customerID)
    {
        var documents = await CustomerFleetManagementService.ViewDocumentAsync(DocumentRepositoryID);
        
        string path = @"C:\Temp2";
        System.IO.Directory.CreateDirectory(path);
        FileStream stream = new FileStream(Path.Combine(path, documents.ImgName), FileMode.Create);
        stream.Write(documents.ImgData, 0, documents.ImgLength);
        stream.Close();
        new ProcessStartInfo(@"C:\Temp2\"+documents.ImgName).StartProcess();
        return RedirectToAction("Files", "Customer", new { customerID = customerID });
    }

  public static class UT
    {
            public static Process StartProcess(this 
            ProcessStartInfoo psi, bool useShellExecute = 
            true)
    {
        psi.UseShellExecute = useShellExecute;
        return Process.Start(psi);
    }
    }
[HttpGet]
公共异步任务下载(int-DocumentRepositoryID、int-customerID)
{
var documents=wait CustomerFleetManagementService.ViewDocumentAsync(DocumentRepositoryID);
字符串路径=@“C:\Temp2”;
System.IO.Directory.CreateDirectory(路径);
FileStream-stream=newfilestream(Path.Combine(Path,documents.ImgName),FileMode.Create);
stream.Write(documents.ImgData,0,documents.ImgLength);
stream.Close();
新进程startInfo(@“C:\Temp2\”+documents.ImgName).StartProcess();
返回重定向操作(“文件”、“客户”,新的{customerID=customerID});
}
公共静态类UT
{
公共静态进程启动进程(此
ProcessStartInfo psi,bool useShellExecute=
(对)
{
psi.UseShellExecute=UseShellExecute;
返回过程。启动(psi);
}
}

你不能,这是浏览器的责任。想象一下下载到启动目录或覆盖内核。只接受代码的答案是不被接受的。请添加注释和解释。此外,您的后端代码将仅在服务器上执行。这仅在客户端和服务器位于同一台计算机上时有效。Ie文件将以路径
c:\temp2\…
保存在服务器上,而不是保存在客户端。@derpirscher因此,如果我将此.net核心应用程序部署到IIS以及访问网站并导航到下载操作结果的任何人,将不会在客户端计算机上创建目录。如何在客户端计算机上创建文件夹。