Search code examples
c#asp.net-corerazor-pages

razor page asp-page-handler button not work


I am implementing an upload download file web server.

So far, I have succeeded in implementing it while watching the MS tutorial. I have implemented a new button (download) function, and I want to know how to execute a specific function with a button on a web page.

I've tried a lot of searches and tried to follow, but it didn't work.

There seems to be a problem with my code or some other condition, but I'm new to web programming, so I don't know what it is.

this cshtml code

@page "{id}"
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@model WebApplication1.Pages.test.BufferedMultipleFileUploadPhysicalModel
@{ ViewData["Title"] = "Buffered Multiple File Upload (Physical)"; }


<h1>Upload multiple buffered files to physical storage with one file upload control</h1>

<p>One or more files can be selected by the visitor. The following form's page handler saves the file(s) to disk.</p>

<h4>graphic_tbl</h4>
<hr />


<form enctype="multipart/form-data" method="post">
    <dl>
        <dt>
            <label asp-for="FileUpload.FormFiles"></label>
        </dt>
        <dd>
            <input asp-for="FileUpload.FormFiles" type="file" multiple />
            <span asp-validation-for="FileUpload.FormFiles"></span>
        </dd>
    </dl>
    <input asp-page-handler="Upload" class="btn" type="submit" value="Upload" />
    <div class="form-group">
        <label for="version" class="col-sm-2 control-label">version</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="version">
        </div>
    </div>
</form>

<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.view_Join_Model[0].ver)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.view_Join_Model[0].date)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.view_Join_Model[0].dateupdate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.view_Join_Model[0].resource_link)
            </th>
            <th>
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.view_Join_Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.ver)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.date)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.dateupdate)
                </td>
                <td>
                    @Html.DisplayFor((modelItem => item.resource_link))

                </td>

                <td>
*this part*
                <input asp-page-handler="DownloadFile" class="btn" type="submit" value="Download" />

                    @*<button type="submit" asp-page-handler="DownloadFile" class="btn btn-outline-info"
                id=item.resourceid>
            Donwload
        </button>*@
                </td>

            </tr>
        }
    </tbody>


</table>


<p class="result">
    @Model.Result
</p>


this cs file

public class BufferedMultipleFileUploadPhysicalModel : PageModel
    {
        [BindProperty]
        public BufferedMultipleFileUploadPhysical FileUpload { get; set; }
        [BindProperty]
        public model_version_tbl model_Version_Tbl { get; set; }
        [BindProperty]
        public resource_tbl resource_Tbl { get; set; }
        public IList<fileJoinModel> view_Join_Model{ get; set; }

        [BindProperty]
        public string version { get; set; }
        public string Result { get; private set; }
        public SelectList seletcList { get; set; }

        public void OnPost()
        {
            
        }
        public FileContentResult OnGetDownloadFile(int resoruceId)
        {
            var querydata =_context.resource_tbl.Find(resoruceId);
           
            byte[] fileBytes = GetFile(querydata.resource_link);
            return File(
                fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, querydata.resource_link);
        }

        byte[] GetFile(string s)
        {
            System.IO.FileStream fs = System.IO.File.OpenRead(s);
            byte[] data = new byte[fs.Length];
            int br = fs.Read(data, 0, data.Length);
            if (br != fs.Length)
                throw new System.IO.IOException(s);
            return data;
        }
    }

    public class BufferedMultipleFileUploadPhysical
    {
        [Required]
        [Display(Name = "File")]
        public List<IFormFile> FormFiles { get; set; }

        [Display(Name = "Note")]
        [StringLength(50, MinimumLength = 0)]
        public string Note { get; set; }
    }
}


Solution

  • 1.Firstly,asp-page-handler cannot work with GET method,you can refer to the link.So you need to change OnGetDownloadFile to OnPostDownloadFile.

    2.Also,asp-page-handler needs to be used with form.You can use the following code:

    <form method="post">
            <input asp-page-handler="DownloadFile" class="btn" type="submit" value="Download" />
        </form>
    

    3.And if you want to get the id in "Upload/{id}".You need to change int resoruceId to int id.