![](/img/trans.png)
[英]C# Html Razor page Print pdf from html with Colors
[英]FormData Values in Razor HTML page, getting in C# Method
我在剃刀視圖頁面中有以下輸入元素
<input type="file" id="uploadFile" name="FileUpload" multiple="multiple" />
使用以下腳本,我將數據綁定到此ForData
$('#uploadFile').on('change', function()
{
var fd = new FormData();
var files = $('#uploadFile')[0].files;
for (var i = 0; i < files.length; i++)
{
if (files[i].size < 5242880)
{
fd.append("myFiles", files[i])
}
}
});
我正在嘗試使用C#方法獲取這些文件,如下所示
[HttpPost]
public ActionResult SomeAction(ModelClass model)
{
var attchaedfiles = System.Web.HttpContext.Current.Request.Files["myFiles"];
for (int i = 0; i < attchaedfiles.Count; i++)
{
if (!string.IsNullOrEmpty(attchaedfiles[i].FileName))
{
....
}
}
}
但在這里我得到以下錯誤
運算符“ <”不能應用於類型為“ int”和“ method group”的操作數
無法將帶有[]的索引應用於類型為'System.Web.HttpPostedFile'的表達式
在ajax腳本中,確保包括以下內容:
$.ajax({
url: yourUrl,
type: 'POST',
// Needed for FormData submit
processData: false,
// Needed for FormData submit
contentType: false,
data: fd,
dataType: 'json',
success: function () {
// Success things
},
error: function () {
// Error things
},
complete: function () {
// Complete things
}
});
並且您的表單代碼包括enctype =“ multipart / form-data”:
@using (Html.BeginForm(null, null, FormMethod.Post, new { @action = YourAction, id = "your_id", enctype = "multipart/form-data" }))
嘗試以下方法檢查有關文件上傳的可能條件:
模型:
public class ExperimentViewModel
{
public int Id { get; set; }
[DataType(DataType.Upload)]
public IEnumerable<HttpPostedFileBase> FileUpload { get; set; }
//other properties
}
控制器:
if (model.FileUpload != null)
{
if (model.FileUpload.Count() > 0)
{
foreach (var upload in model.FileUpload)
{
if (upload != null && upload.ContentLength > 0)
{
//your stuff
}
}
}
}
您也可以查看有關如何在MVC中顯示圖像的答案。
希望這可以幫助...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.