1

asp.net mvc2 を使用して履歴書をアップロードおよびダウンロードしたいと考えています。私はすでにコーディングを作成しました。正常にアップロードされました。ファイルをダウンロードしようとすると、問題が発生します.空のページが表示されます..

コントローラ:

 [HandleErrorWithAjaxFilter]
    public ActionResult UploadResume(HttpPostedFileBase FileData)
    {
        Stream fromStream = FileData.InputStream;
        Stream toStream = new FileStream(Server.MapPath("~/Content/Resumes/") + FileData.FileName, FileMode.Create);

        LoggedInCandidate.ResumeFileName = FileData.FileName;
        //_repository.Save();
        _userRepository.Save();

        return Json(new JsonActionResult
        {
            Success = true,
            Message = "Resume has been uploaded."
        });
        //return Json("Resume has been uploaded.");
    }

意見:

    <input id="Resume" type="file" name="Resume" />     

ダウンロード:

<p>
                <% var link = Url.Content("~/Content/Resumes/") + Model.ResumeFileName; %>
                <a href="<%: link %>">Download Resume</a> 
            </p>

リンクのダウンロード再開をクリックすると、URL にファイルの名前が表示されますが、ダウンロードされません。

4

2 に答える 2

0

これを行う方法は次のとおりです。次のようにコントローラーでアクションを作成します。

public FileResult Download(string fileName)
{
    var path = Path.Combine(Server.MapPath("~/Content/Resumes/"), fileName);
    var fileStream = new FileStream(path, FileMode.Open);

    // Assuming that the resume is an MS Word document...
    return File(fileStream, "application/vnd.ms-word");
}

そして、ビューには次のものがあります。

<p>
  <%: Html.ActionLink("Download Resume", "Download", new { fileName = Model.ResumeFileName }) %>
</p>
于 2013-08-14T20:34:25.413 に答える
-1

コントローラ:

 [Authorize]
    public ActionResult Download(string fileName)
    {
        string pfn = Server.MapPath("~/Content/Resumes/" + fileName);

        if (!System.IO.File.Exists(pfn))
        {
            //throw new ArgumentException("Invalid file name or file not exists!");

            return Json(new JsonActionResult { Success = false, Message = "Invalid file name or file not exists!" });
        }
        else
        {

            return new BinaryContentResult()
            {
                FileName = fileName,
                ContentType = "application/octet-stream",
                Content = System.IO.File.ReadAllBytes(pfn)
            };
        }

    }

モデル: /BinaryFileResult

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mvc;

名前空間 Dial4Jobz.Models { public class BinaryContentResult : ActionResult {

    public BinaryContentResult()
    {
    }

    public string ContentType { get; set; }
    public string FileName { get; set; }
    public byte[] Content { get; set; }

    public override void ExecuteResult(ControllerContext context)
    {

        context.HttpContext.Response.ClearContent();
        context.HttpContext.Response.ContentType = ContentType;

        context.HttpContext.Response.AddHeader("content-disposition",

        "attachment; filename=" + FileName);

        context.HttpContext.Response.BinaryWrite(Content);
        context.HttpContext.Response.End();
    }
}

}

于 2013-08-22T06:57:28.630 に答える