剃刀页面asp页面处理程序按钮不工作



我正在实现一个上传-下载文件的web服务器。

到目前为止,我已经在观看MS教程的同时成功地实现了它。我实现了一个新的按钮(下载(功能,我想知道如何在网页上用按钮执行特定的功能。

我试了很多次搜索并试图跟踪,但都没有成功。

我的代码或其他情况似乎有问题,但我是网络编程的新手,所以我不知道它是什么。

这个cshtml代码

@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>

这个cs文件

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

1.首先,asp-page-handler不能与GET方法一起使用,可以参考链接。因此,您需要将OnGetDownloadFile更改为OnPostDownloadFile

2.此外,asp-page-handler需要与表单一起使用。您可以使用以下代码:

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

3.如果您想在"Upload/{id}"中获取id。您需要将int resoruceId更改为int id

最新更新