gpt4 book ai didi

c# - 部分查看文件上传

转载 作者:行者123 更新时间:2023-11-30 23:31:25 25 4
gpt4 key购买 nike

我正在尝试弄清楚如何使用局部 View 存储多个文件。可以看到局部 View 的截图 here .

只要我使用常规表单输入,部分 View 本身就可以完美运行。然而,存储文件有点复杂。由于我使用的是 Entity First,因此我只是存储对文件的引用而不是实际文件本身。这适用于单个文件。字节引用存储到我的数据库中,我可以在显示特定记录的所有属性的 View 中生成文件链接。但是,如果我想要多次上传,我对如何解决这个问题一无所知。每添加一行一个。

我也不确定我应该如何通过 jQuery 定位某一行的单独字段,因为所有行都是动态生成的,但这是一个不同的问题。

相关代码:

我的 Controller 的 POST 操作:

    [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateSrv([Bind(Include = "ID,CreationTimestamp,EditTimestamp,requestStatus,Requester,Reponsible,DeliveryDate,Project,CostCenter, MailCounter, ExtendedOrders")] Request request, HttpPostedFileBase upload)
{

if (ModelState.IsValid)
{
if (upload != null && upload.ContentLength > 0)
{
if (upload.ContentLength > 0)
{
var fileName = Path.GetFileName(upload.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
upload.SaveAs(path);

using (var reader = new System.IO.BinaryReader(upload.InputStream))
{
request.ExtendedOrders.ToArray()[0].File = reader.ReadBytes(upload.ContentLength);
}
}
}

request.CreationTimestamp = DateTime.Now;
request.EditTimestamp = DateTime.Now;
request.RequestStatus = "Initial";
request.RequestType = "Services";
db.requests.Add(request);
db.SaveChanges();

return RedirectToAction("Index");
}
return View(request);
}

使用 AJAX 向我的局部 View 添加行的 Controller 方法:

    [HttpGet]
public ActionResult AddOrderRowExtended()
{
OrderExtended order = new OrderExtended();
order.Units = GetSelectListItems(GetUnitsFromDB());
order.Unit = "PC";
order.Currencies = GetSelectListItems(GetCurrenciesFromDB());
order.Currency = "European Euro";
return PartialView("~/Views/Orders/OrderExtendedCreate.cshtml", order);
}


然后我在“主” View 中调用局部 View 。 addOrder 按钮动态添加更多行:

@using (Html.BeginForm("CreateSrv", "Requests", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()


<div class="form-horizontal">
... some fields of my Request model...

<tbody id="OrderZone">
@foreach (var row in Model.ExtendedOrders)
{
Html.RenderPartial("~/Views/Orders/OrderExtendedCreate.cshtml", row);
}
</tbody>
<button class="btn btn-sm btn-success" type="button" id="addOrder">Add row</button>
<br />
<div class="body-content">
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
</div>

我的 OrderExtended 模型:

public class OrderExtended
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }


....

[NotMapped]
[DataType(DataType.Upload)]
public HttpPostedFileBase Quotation { get; set; }

public byte[] File { get; set; }

//FK
public int RequestRefId { get; set; }

[ForeignKey("RequestRefId")]
public virtual Request Request { get; set; }
}

OrderExtendedCreate.cshtml

@model RFP_MVC.Models.OrderExtended



<tr class="editorRow">
@using (Html.BeginCollectionItem("ExtendedOrders"))
{
<td class="col-md-2">@Html.EditorFor(model => model.Material, new { htmlAttributes = new { @class = "input-sm form-control" } })</td>
<td class="col-md-1">@Html.EditorFor(model => model.Quantity, new { htmlAttributes = new { @class = "input-sm form-control" } })</td>
<td class="col-md-2">@Html.DropDownListFor(model => model.Unit, Model.Units, new { id = "Unit", @class = "input-sm form-control select2" })</td>
<td class="col-md-2">@Html.DropDownListFor(model => model.PurchGroup, Model.PurchGroupList, "Select...", new { @class = "input-sm form-control" })</td>
<td class="col-md-2">
<div class="input-group">
<span class="input-group-btn">
<button class="btn btn-sm btn-default" type="button" data-toggle="modal" data-target="#myModal"><span class="glyphicon glyphicon-search"></span></button>
</span>
@Html.EditorFor(model => model.Vendor, new { htmlAttributes = new { id = "Vendor" , @class = "input-sm form-control" } })
</div>
</td>
<td class="col-md-1">@Html.EditorFor(model => model.OrderingCode, new { htmlAttributes = new { @class = "input-sm form-control" } })</td>
<td class="col-md-1">@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "input-sm form-control" } })</td>
<td class="col-md-1">@Html.DropDownListFor(model => model.Currency, Model.Currencies, new { id = "Currency", @class = "input-sm form-control select2" })</td>
<td class="col-md-2">
<span class="btn btn-sm btn-default btn-file glyphicon glyphicon-folder-open"><input type="file" name="upload"></span>
</td>
}
</tr>

提前致谢

更新了解决方案:

更改了我的 POST 方法

  1. 将参数更改为 IEnumerable(按照 ramiramilu 的建议):

    IEnumerable 文件

  2. 遍历所有文件并处理它们:

int fileIndex = 0;
foreach (HttpPostedFileBase upload in files)
{
if (upload != null && upload.ContentLength > 0)
{
var fileName = Path.GetFileName(upload.FileName);
var path = Path.Combine(Server.MapPath("~/uploads"), fileName);
upload.SaveAs(path);

using (var reader = new System.IO.BinaryReader(upload.InputStream))
{
request.ExtendedOrders.ToArray()[fileIndex].File = reader.ReadBytes(upload.ContentLength);
}

fileIndex++;
}
}

最佳答案

如果你想上传多个文件,你需要期待 IEnumerable<HttpPostedFileBase> upload作为您的操作的参数。一旦你得到 IEnumerable<HttpPostedFileBase> upload , 使用 foreach并保存每个文件。

关于c# - 部分查看文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34588095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com