2

私の MVC には、1 つのファイル アップロード コントロールと 1 つのボタンを含むビューがあります。

 <input type="file" id="Uploadfile" />
 <input type="button" onclick()="GetFile();/>

Javascript関数は次のとおりです

  function GetFile()
  {
      var file_data = $("#Uploadfile").prop("files")[0];
      window.location.href="Calculation/Final?files="+file_data;
  }

選択したファイルを fileupload コントロールを介して mvc のコントローラーに渡す/送信する必要があります。私はコントローラーを持っています

public ActionResult Final(HttpPostedFileBase files)
  {
     //here i have got the files value is null.
  }

選択したファイルを取得してコントローラーに送信する方法は? この問題を解決するのを手伝ってください。

4

5 に答える 5

7

私のプロジェクトで提供する同様の機能がありました。作業コードは次のようになります。

コントローラ クラス

[HttpPost]
public ActionResult UploadFile(YourModel model1)
{
    foreach (string file in Request.Files)
    {
        HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
        if (hpf.ContentLength > 0)
        {
            string folderPath = Server.MapPath("~/ServerFolderPath");
            Directory.CreateDirectory(folderPath);

            string savedFileName = Server.MapPath("~/ServerFolderPath/" + hpf.FileName);
            hpf.SaveAs(savedFileName);
            return Content("File Uploaded Successfully");
        }
        else
        {
            return Content("Invalid File");
        }
        model1.Image = "~/ServerFolderPath/" + hpf.FileName;
    }

    //Refactor the code as per your need
    return View();
}

意見

@using (@Html.BeginForm("UploadFile", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
 <table style="border: solid thin; margin: 10px 10px 10px 10px">
     <tr style="margin-top: 10px">
         <td>
             @Html.Label("Select a File to Upload")
             <br />
             <br />
             <input type="file" name="myfile">
             <input type="submit" value="Upload" />
         </td>
     </tr>
 </table>
}
于 2014-03-17T14:12:20.070 に答える
3

javascript 経由でファイル コンテンツを送信することはできません (HTMl5 を除く)。そして、あなたは完全に間違っています。FileReader api を介して HTML5 ベースのソリューションを実行する場合は、これを確認する必要があります。FileReader API

フォームタグを配置し、コントローラーアクションで同じ名前の入力を使用して、モデルバインディングを実行するだけです

@using(Html.BeginForm("yourAction","YourControl",FormMethod.Post))
{
 <input type="file" id="fileUpload" />
}

次にコントローラーで。

[HTTPPost]
public ActionResult Final(HttpPostedFileBase fileUpload)
  {
     //here i have got the files value is null.
  }
于 2013-09-25T05:39:21.803 に答える
1

以下のコードは、ajax ファイルのアップロードの錯覚を与える非表示のフォームで完全なポスト バックを行います。それを試してみてください:

アップデート:

JS

function Upload(sender) {
    var iframe = $("<iframe>").hide();
    var newForm = $("<FORM>");
    newForm.attr({ method: "POST", enctype: "multipart/form-data", action: "/ControllerName/Final" });        
    var $this = $(sender), $clone = $this.clone();
    $this.after($clone).appendTo($(newForm));
    iframe.appendTo($("html")).contents().find('body').html($(newForm));
    newForm.submit();
}

HTML

<input type="file" id="Uploadfile" name="Uploadfile" />
<input type="button" onclick="Upload($('#UploadFile'));"/>

コントローラ

public ActionResult Final(HttpPostedFileBase Uploadfile)
{
     //here you can use uploaded file
}
于 2013-09-25T05:51:41.230 に答える
0

Ravi の回答の完成として、次のusingステートメントを使用することをお勧めします。

@using(Html.BeginForm("yourAction","YourControl",FormMethod.Post, new { enctype="multipart/form-data" }))
{
   <input type="file" id="fileUpload" />
}
于 2013-09-25T05:45:09.043 に答える