简体   繁体   English

您可以从 Windows 窗体应用程序中的 HttpContent 流下载文件吗?

[英]Can you download a file from an HttpContent stream from within a Windows Forms Application?

I recently developed a .NET Web App that downloaded zip files from a certain, set location on our network.我最近开发了一个 .NET Web 应用程序,它从我们网络上的某个设置位置下载 zip 文件。 I did this by retrieving the content stream and then passing it back to the View by returning the File().我通过检索内容流然后通过返回 File() 将其传回 View 来做到这一点。

Code from the .NET Web App who's behavior I want to emulate:来自 .NET Web App 的代码我想模仿谁的行为:

public async Task<ActionResult> Download()
    {
        try
        {
            HttpContent content = plan.response.Content;
            var contentStream = await content.ReadAsStreamAsync(); // get the actual content stream

            if (plan.contentType.StartsWith("image") || plan.contentType.Contains("pdf"))
                return File(contentStream, plan.contentType);

            return File(contentStream, plan.contentType, plan.PlanFileName);
        }
        catch (Exception e)
        {
            return Json(new { success = false });
        }
    }

plan.response is constructed in a separate method then stored as a Session variable so that it is specific to the user then accessed here for download. plan.response 在单独的方法中构建,然后存储为 Session 变量,以便它特定于用户,然后在此处访问以进行下载。

I am now working on a Windows Forms Application that needs to be able to access and download these files from the same location.我现在正在开发一个 Windows 窗体应用程序,它需要能够从同一位置访问和下载这些文件。 I am able to retrieve the response content, but I do not know how to proceed in order to download the zip within a Windows Forms Application.我能够检索响应内容,但我不知道如何继续以在 Windows 窗体应用程序中下载 zip。

Is there a way, from receiving the content stream, that I can download this file using a similar method within a Windows Form App?有没有办法从接收内容流开始,我可以在 Windows 窗体应用程序中使用类似的方法下载此文件? It would be convenient as accessing the files initially requires logging in and authenticating the user and thus can not be accessed normally with just a filepath.这会很方便,因为访问文件最初需要登录并验证用户,因此不能仅通过文件路径正常访问。

Well, depending on what you're trying to accomplish, here's a pretty simplistic example of download a file from a URL and saving it locally:好吧,根据您要完成的任务,这里有一个非常简单的示例,从 URL 下载文件并将其保存在本地:

        string href = "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-zip-file.zip";
        WebRequest request = WebRequest.Create(href);
        using (WebResponse response = request.GetResponse())
        {
            using (Stream dataStream = response.GetResponseStream())
            {
                Uri uri = new Uri(href);
                string fileName = Path.GetTempPath() + Path.GetFileName(uri.LocalPath);
                using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate))
                {
                    dataStream.CopyTo(fs);
                }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您可以从Windows窗体应用程序公开XML Web服务吗? - Can you expose XML webservices from a Windows Forms Application? 如何将流从HttpContent结果上载到Azure文件存储 - How to upload the Stream from an HttpContent result to Azure File Storage 从 Windows Forms 应用程序执行批处理文件 - Executing a batch file from Windows Forms application 如何在Windows窗体应用程序中的c#中将标准输出重定向到psexec进程中的文件 - How to redirect standard output to a file from psexec process in c# within a Windows Forms Application 无法从POST请求反序列化HttpContent - Can not deserialize HttpContent from POST request 如何在测试中创建文件并将其转换为 HttpContent 对象? - How can I create a file within a test and convert it to a HttpContent object? 从C#表单应用程序中执行远程批处理文件 - Execute remote batch file from within a C# forms application 如何在WinForm应用程序中从Web下载文件 - How to download a file from the web within a WinForm application 动态显示Windows窗体应用程序中资源文件中的图像 - Dynamically show an image from a resource file in a Windows Forms application 你能从C#控制台应用程序中执行另一个EXE文件吗? - Can you execute another EXE file from within a C# console application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM