2013-08-10 3 views
1

Я хочу загрузить и загрузить резюме с помощью asp.net mvc2. Я уже создал кодировки. Он загружен успешно. Когда я пытаюсь загрузить файл У меня есть вопрос .. Он показывает пустую страницу ..Загрузите файл mvc2 с помощью actionlink

Контроллер:

[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-адресу, но он не загружается.

ответ

-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 { общественного класса 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(); 
    } 
} 

}

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> 
+0

Это не загружая .. что делать? pls help me – PoliDev

+0

Что происходит точно? Вы получаете объект FileStream? – ataravati

+0

Если я нажму ссылку для загрузки, показывая пустую страницу. Я использую Google Chrome. нет возможности открытия загрузки. – PoliDev

Смежные вопросы