使用ASP.NET Core Razor页面下载文件 [英] Download a file with ASP.NET Core Razor page

查看:46
本文介绍了使用ASP.NET Core Razor页面下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说明编辑我创建了一个新项目,试图将我原来的MVC项目转换为现在仅使用Razor页面.我最初的解决方案是此处...>

我花了一些时间完成转换以显示文档列表,但我现在已经完成了.我一直在尝试下载该文件,但它一直告诉我该文件不存在,即使它已列出.

错误消息

 未找到以下网址的网址:https://localhost:5001/FileShare/DownloadStub?id = SCHWADERER_PayStub_191018_1026.pdf 

这是我的模特

FileDataModel.cs

 公共类FileDataModel{公共字符串FileName {get;放;}公开字串大小{放;}公共字符串DateModified {get;放;}公共字符串ParentDirName {get;放;}公共字符串SubDirName {get;放;}} 

我在页面后面的代码

FileShare.cshtml.cs

 公共异步任务< IActionResult>DownloadStub(字符串ID){使用MemoryStream memoryStream = new MemoryStream();字符串fileStorageConnection = _configuration.GetValue< string>("fileStorageConnection");CloudStorageAccount storageAccount = CloudStorageAccount.Parse(fileStorageConnection);CloudFileShare份额= storageAccount.CreateCloudFileClient().GetShareReference("payreports");CloudFileDirectory rootDir = share.GetRootDirectoryReference();CloudFileDirectory dir = rootDir.GetDirectoryReference(@"E000001/stubs");CloudFile文件= dir.GetFileReference(id);等待文件.DownloadToStreamAsync(memoryStream);流fileStream = file.OpenReadAsync().Result;返回File(fileStream,file.Properties.ContentType,file.Name);} 

最后是我在网页上的代码

FileShare.cshtml

  table class ="table table-bordered">< thead>< tr>< th>文件名</th>< th>文件大小</th>< th>文件日期</th>< th>下载</th></tr></thead>< tbody>@foreach(Model.FileDataModels中的var数据){< tr>< td> @ data.FileName</td>< td> @ data.Size</td>< td> @ data.DateModified</td>< td>< a class ="btn btn-primary btn-sm".href ="/FileShare/DownloadStub?id=@data.FileName"> Download</a></td></tr>}</tbody></table> 

我没有将正确的值传递给href吗?

我还需要获取其他一些价值吗?

是否应该使用taghelper来完成?

我不确定正在进行的事情和需要做的事情,以便朝正确的方向前进.任何提示都将非常感谢

解决方案

该错误消息指出,它找不到您的网址,与文件无关.默认情况下,该网址为控制器/操作/参数,而不是查询字符串,因此您的网址应为/FileShare/DownloadStub/@data.FileName

但是我建议为此使用RAZOR,因为它允许您在将来将来自定义应如何创建URL的情况下正确运行,即使不确定如何用作控制器名(可能是FileShare):

  @ Html.ActionLink("Description","DownloadStub","FileShare",新的{id = data.FileName},null); 

Clarification edit I have created a NEW project trying to convert my original MVC project to now only use Razor pages. My original solution is Here.

It took me a while to get the conversion done to display a list of the documents but I have that completed now. I've been working on trying to get the file to download but it keeps telling me that the file doesn't exist, even though it's listed.

ERROR message

No webpage was found for the web address: https://localhost:5001/FileShare/DownloadStub?id=SCHWADERER_PayStub_191018_1026.pdf

Here is my Model

FileDataModel.cs

    public class FileDataModel
    {
        public string FileName { get; set; }
        public string Size { get; set; }
        public string DateModified { get; set; }
        public string ParentDirName { get; set; }
        public string SubDirName { get; set; }  
    }

My code behind the Page

FileShare.cshtml.cs

public async Task<IActionResult> DownloadStub(string id)
        {
            using MemoryStream memoryStream = new MemoryStream();
            string fileStorageConnection = _configuration.GetValue<string>("fileStorageConnection");
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(fileStorageConnection);
            CloudFileShare share = storageAccount.CreateCloudFileClient().GetShareReference("payreports");
            CloudFileDirectory rootDir = share.GetRootDirectoryReference();
            CloudFileDirectory dir = rootDir.GetDirectoryReference(@"E000001/stubs");
            CloudFile file = dir.GetFileReference(id);

            await file.DownloadToStreamAsync(memoryStream);
            Stream fileStream = file.OpenReadAsync().Result;
            return File(fileStream, file.Properties.ContentType, file.Name);
            
        }

And finally my code on the Webpage

FileShare.cshtml

table class="table table-bordered">
    <thead>
    <tr>
        <th>File Name</th>
        <th>File Size</th>
        <th>File Date</th>
        <th>Download</th>
    </tr>
    </thead>
    <tbody>
    @foreach (var data in Model.FileDataModels)
    {
        <tr>
            <td>@data.FileName</td>
            <td>@data.Size</td>
            <td>@data.DateModified</td>
            <td><a class="btn btn-primary btn-sm"
                    href="/FileShare/DownloadStub?id=@data.FileName">Download</a></td>
        </tr>
    }
    </tbody>
</table>

Am I not passing the right value into the href?

Is there some other value I need to be capturing?

Should this be done using a taghelper?

I'm not sure what's going on and what I need to do so that I head in the right direction. Any tips would be greatly appreciated!

解决方案

The error message says that it couldn't find your url, nothing about the file. By default the url goes as Controller/Action/Parameter instead of query strings, so your URL should be /FileShare/DownloadStub/@data.FileName

But I would recommend to use RAZOR for that because it allows you to function correctly if you will, in the future, customize how your URL should be created which goes like this, even though not sure what to use as a controllername (probably FileShare):

@Html.ActionLink("Description", "DownloadStub", "FileShare", new { id = data.FileName}, null);

这篇关于使用ASP.NET Core Razor页面下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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