简体   繁体   中英

Download a file with ASP.NET Core Razor page

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM