Bootstrap文件输入插件将表单数据获取到控制器

时间:2015-02-04 06:22:41

标签: jquery asp.net-mvc c#-4.0

我正在使用Bootstrap File Input plugin而我正在尝试将图像传输给Controller,但我没有取得任何成功。有没有人在MVC中实现这个可以给我一些方向?我成功传递了uploadExtraData属性中的contactId。如果我将其注释掉并使用data属性,则会收到一条错误消息,指出无法找到该方法的无参数构造函数。这是有道理的,但我不知道如何将输入数组传递给控制器​​。

查看代码

<div class="col-md-12 form-group">
    <div class="editor-field">
        <input id="input-702" name="kartik-input-702[]" type="file" multiple="true" class="file-loading">
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function() {
        $("#input-702").fileinput({
            type: 'POST',
            cache: false,
            allowedFileExtensions: ['jpg', 'png', 'gif'],
            allowedFileTypes: ['image'],
            maxFileSize: 2000,
            uploadUrl: '@Url.Action("ImageUpload", "Contact")',
            maxFileCount: 2,
            enctype: 'multipart/form-data',
            overwriteInitial: true,
            uploadExtraData: { 'request': document.getElementById('input-702').value, 'contactId' : document.getElementById('ContactID').value },
            msgSizeTooLarge: "File {name} ({size} KB) exceeds maximum upload size of {maxSize} KB. Please Try again",
            msgFilesTooMany: "Number of Files selected for upload ({n}) exceeds maximum allowed limit of {m}",
            msgInvalidFileType: 'Invalid type for file "{name}". Only {types} files are supported.',
            msgInvalidFileExtension: 'Invalid extension for file {name}. Only "{extensions} files are supported.',
        });
    });
</script>

控制器代码

[HttpPost]
public ActionResult ImageUpload(HttpRequest request, int contactId)
{
    int contactID = (int)contactId;
    return null;
}

1 个答案:

答案 0 :(得分:2)

解决:我发现了一个使用MVC实现的类似plugin。相同的结构适用于此控件。

public ActionResult SaveUploadedFile()
    {
        bool isSavedSuccessfully = true;
        string fName = "";
                try{
        foreach (string fileName in Request.Files)
        {
            HttpPostedFileBase file = Request.Files[fileName];
            //Save file content goes here
            fName = file.FileName;
            if (file != null && file.ContentLength > 0)
            {

                var originalDirectory = new DirectoryInfo(string.Format("{0}Images\\WallImages", Server.MapPath(@"\")));

                string pathString = System.IO.Path.Combine(originalDirectory.ToString(), "imagepath");

                var fileName1 = Path.GetFileName(file.FileName);

                bool isExists = System.IO.Directory.Exists(pathString);

                if (!isExists)
                    System.IO.Directory.CreateDirectory(pathString);

                var path = string.Format("{0}\\{1}", pathString, file.FileName);
                file.SaveAs(path);

            }

        } 

       }
       catch(Exception ex)
        {
            isSavedSuccessfully = false;
        }


        if (isSavedSuccessfully)
        {
            return Json(new { Message = fName });
        }
        else
        {
            return Json(new { Message = "Error in saving file" });
        }
    }