1

在 mvc3 应用程序中,Product 具有字符串类型的 ImagePath1、ImagePath2、ImagePath3、ImagePath4 和 ImagePath5 属性。我可以在这条规则中保存 1 张图片。对于 1 张图像,在控制器中:

public ActionResult Create( Product product, HttpPostedFileBase file )
{
       var filename = Path.GetFileName( file.FileName );
       var path = Path.Combine( Server.MapPath( "~/UploadFolder/Products" ), filename );
       file.SaveAs( path );
       product.ImagePath1 = filename;
       db.Products.AddObject( product );
       db.SaveChanges();
}

我有文件上传示例:

<script type="text/javascript">
    function HandleFileButtonClick() {
        document.frmUpload.myFile.click();
        document.frmUpload.txtFakeText.value = document.frmUpload.myFile.value;
    }
</script>
<form name="frmUpload">
<input type="file" name="myFile" style="display: none;">
<input type="text" name="txtFakeText" readonly="true">
<input type="button" onclick="HandleFileButtonClick();" value="Select" class="UploadButton">
</form>

但我不能在产品的创建视图中应用它。

@using ( Html.BeginForm( "Create", "Product", FormMethod.Post, new { enctype = "multipart/form-data" } ) )
{    
     @Html.TextBoxFor( m => m.Name, new { @class = "createInputStyle" } )
     ...                        
     ...// Images here...
     ...
     <input type="submit" value="Create" class="" />
}

文件上传示例在外部工作,但是当我将上传表单放入创建表单时,出现问题。如何保存更多图像?抱歉英语不好。

4

1 回答 1

2

将以下内容粘贴到您的 Html.BeginForm() {}

<input type='file' name='ImagePath1' /> 
<input type='file' name='ImagePath2' /> 
<input type='file' name='ImagePath3' /> 
<input type='file' name='ImagePath4' /> 
<input type='file' name='ImagePath5' />

然后,在您的控制器上,检查Request.Files应该保存这些图像的 (假设已上传图像)

于 2012-08-30T05:55:40.233 回答