5

私は ASP.NET MVC 4 を使用しており、アップロードされたファイルを開いて操作するために、そのパスを取得しようとしています。これが私が進める方法です:

コントローラ

public ActionResult Bulk(HttpPostedFileBase file)
{
    FileStream fs = System.IO.File.Open(Server.MapPath(file.FileName), 
                                         FileMode.Open, FileAccess.Read);

    return RedirectToAction("Index");
}

意見

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

    @using (Html.BeginForm("Bulk", "Bulk", null, FormMethod.Post, new 
                                          { enctype = "multipart/form-data" }))
    {
        @Html.ValidationSummary(true)

        <fieldset>
            <p>
                <input type="file" name="file"/>
            </p>
            <div class="form-actions">
              <button type="submit" class="btn btn-primary">Create</button>
            </div>
        </fieldset>
    }

私がそれをしているとき、私は言うエラーを受け取ります...Could not find a part of the path ...

ファイルが実際に配置されているパスを取得するにはどうすればよいですか?

4

2 に答える 2

4

私が理解しているように、ファイルを開く前にサーバーにアップロードする必要があります。

 if (file != null && file.ContentLength > 0) 
    {
        // extract only the fielname
        var fileName = Path.GetFileName(file.FileName);
        // store the file inside ~/App_Data/uploads folder
        var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
        file.SaveAs(path);
    }

上記は、Chance と Darin Dimitrov による同様の質問の回答から取得しました: File Upload ASP.NET MVC 3.0

この回答は、 ASP.NET MVC を使用してファイル (またはファイル) をアップロードする 便利なブログ投稿をさらに参照しています。

于 2013-07-26T09:05:57.533 に答える