开发者

ASP.NET MVC Multiple File Uploads

开发者 https://www.devze.com 2023-01-25 15:37 出处:网络
I\'m working 开发者_运维知识库on a strongly-typed edit form for a MVC model that contains a collection of child models (Document contains many LocalizedDocuments).Each document can contain 0, 1, or ma

I'm working 开发者_运维知识库on a strongly-typed edit form for a MVC model that contains a collection of child models (Document contains many LocalizedDocuments). Each document can contain 0, 1, or many localized documents, and every localized document has an input type="file" to allow the user to upload a new version of the file.

To render the edit fields for each LocalizedDocument I have a strongly-typed partial view that contains the fields for the LocalizedDocument, and then in my edit view use Html.EditorFor(model => model.Document.LocalizedDocuments).

When the form is posted, each of my LocalizedDocument fields are prefixed with LocalizedDocument[i], as expected. However, how can I get it so the file input is also prefixed with LocalizedDocument[i], so I can relate the file upload to the appropriate LocalizedDocument?


Tell me if I'm interpreting this question wrong. You have documents that you need to upload that belongs to a parent table or you need to assign these documents a specific id after uploading multiple of them?

I'll be using Uploadify.

You will have to know the Id of the LocalizedDocument. You will also only be able to upload 1 at a time. (each file is a separate server call)

First you have to assign the Id of the LocalizedDocument to the data that will be sent to the server.

<script type="text/javascript">
var auth = "<%: Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>";
var ASPSESSID = "<%: Session.SessionID %>";

$(document).ready(function () {
    $('#file_upload').uploadify({
        'uploader': '/Content/uploadify.swf',
        'script': '<%: Url.Action("Upload", "Documents") %>',
        'cancelImg': '/Content/Images/cancel.png',
        'folder': '/uploads',
        'scriptData': { ASPSESSID: ASPSESSID, AUTHID: auth, DocumentId: <%: Model.LocalizedDocumentId %>},
        'auto':true,
        'multi': true
    });
});
</script>

Controller:

public ActionResult InsertImages(HttpPostedFileBase FileData, int DocId){ }
0

精彩评论

暂无评论...
验证码 换一张
取 消