1

複数のファイルをアップロードできるファイルアップロードボタンがあります。これらのファイルを JavaScript を使用してフォルダーに保存し、これらのファイルの詳細をビューからコントローラーに送信したいと考えています。私はmvc 4のかみそりアプリをやっています。MVC は初めてです。json および ajax の post メソッドで実行できることはわかっています。しかし、これを使用する方法がわかりません。

 <script type="text/javascript">
       var fileNames = [];
       function handleFileUpload(evt) {
                     evt.stopPropagation();
                     evt.preventDefault();
                     var files = document.getElementById('file1').files;
                     for (var i = 0; files[i]; i++) {
                         fileNames.push(files[i]);
                     }
                  }
            $(function () {
                        $("#btnSubmit").click(function () {
                           $.post("@Url.Action("FileDetails")", { filename: JSON.stringify(fileNames) }, "json");
                      });
                 });
</script>

これが私がこれまで行ってきたことです。

4

1 に答える 1

2

I use a jquery plugin called Uploadify

HTML:

<input type="file" id="uploadBtn" />

Javascript:

<script type='javascript/text'>
$('#uploadBtn').uploadify({
        'uploader': '/uploadify/uploadify.swf',
        'script': 'URL',
        'cancelImg': '/uploadify/cancel.png',
        'buttonText': 'Upload',
        'auto': true,
        'multi': false,
        'removeCompleted': true,
        'simUploadLimit': 1,
        'scriptData': {  },
        'onAllComplete': function () {
           //finished
        }
    });
</script>

MVC ACTION:

public void UploadFile(){
 //Get the file
 HttpPostedFileBase upload = this.Request.Files[0];

 //DO STUFF
}

The url in the javascript method for the parameter 'script' will just be the url to your action. For example if the UploadFile action is in the controller Files then the url will be something like this:

/Files/UploadFile

You can also pass though extra data with 'scriptData' parameter and then just access them the following way

String name = Request["name"];
于 2013-07-08T06:04:43.260 に答える