![](/img/trans.png)
[英]I have multiple labels how do I go to loop through them programmatically in C#
[英]How to upload multiple files into C# and loop through them in MVC
我在將多個文件上傳到C#並循環遍歷它們時遇到問題。 我發現了一個類似問題的問題,但沒有人回答。
我通過html輸入上傳了多個文件。 文件計數以正確的數量(相對於我選擇的文件數量)傳遞到我的控制器。 但是,當我嘗試遍歷每個文件時,它會遍歷第一個相對於文件計數的文件。 例如,如果我上傳3個文件,它將循環瀏覽第一個文件3次,或者如果我上傳5個文件,則循環瀏覽第一個文件5次。 它不應該這樣做。 我希望它逐個循環遍歷每個文件。 請幫忙!!! 這是我的代碼:
Razor View(我正在使用引導程序庫來進行名為bootstrap-filestyle的文件輸入):
@using (Html.BeginForm("Initial", "DataCapture", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
<h4>Select files</h4>
<input type="file" multiple="multiple" id="fileToUpload" name="file">
</div>
<div>
<input type="submit" value="Capture" class="btn btn-default" />
</div>
}
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Initial(ClaimModel claimModel)
{
if (ModelState.IsValid)
{
//OtherCode
handleFile(Request.Files, c.ClaimID);
return RedirectToAction("Index", "ClaimFiles", new { id = c.ClaimID });
}
//OtherCode
}
private void handleFile(HttpFileCollectionBase Files, int ClaimID)
{
foreach (string fileName in Files)
{
HttpPostedFileBase file = Request.Files[fileName];
//OtherCode
}
//OtherCode
}
我在handleFile(Request.Files, c.ClaimID);
上放置了斷點handleFile(Request.Files, c.ClaimID);
在foreach (string fileName in Files){...}
內部foreach (string fileName in Files){...}
。 正如我所說,如果我上傳3、4、5或任意數量的文件,它將正確地通過文件計數,但是它只會循環訪問該數量的第一個文件,並多次return
第一個文件。 我需要它遍歷並分別返回每個文件。 如何正確執行此操作?
使用來自服務器的請求 ,這是一個示例:
for (int i = 0; i < Request.Files.Count; i++)
{
var fileDoc = Request.Files[i];
}
或者,您可以嘗試執行以下操作:
HttpPostedFileBase hpf = null;
foreach (string file in Request.Files)
{
hpf = Request.Files[file] as HttpPostedFileBase;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.