2016-04-26 2 views
-1
MVC

У меня есть эта функция:Невозможно загрузить файл на живой сайт 5

public ActionResult Download(string FileName) 
    { 

     if (FileName != null) 
     { 
      string filepath = AppDomain.CurrentDomain.BaseDirectory + "/uploads/" + FileName; 
      byte[] filedata = System.IO.File.ReadAllBytes(filepath); 
      string contentType = MimeMapping.GetMimeMapping(filepath); 

      var cd = new System.Net.Mime.ContentDisposition 
      { 
       FileName = FileName, 
       Inline = true, 
      }; 

      Response.AppendHeader("Content-Disposition", cd.ToString()); 

      return File(filedata, contentType); 
     } 
     return RedirectToAction("Index"); 
    } 

Тот же код, работающий в местном сервере, но не в реальном сервере, может кто-нибудь сказать мне причину?

+0

первого местом я смотрел является 'filepath' – jamiedanq

+0

Вы можете попробовать это следующим образом: Удалить строку «string filepath = ...» и обновить «byte [] filedata = ...» строка с: byte [] filedata = System.IO.File.ReadAllBytes (Server.MapPath ("~/uploads /" + FileName + "")); – kkakkurt

+1

Пожалуйста, прочитайте [ask] и объясните, как именно это «не работает». – CodeCaster

ответ

0

Это может б не ваш ответ, но вы можете написать все это в одной строке с помощью return File(path, System.Net.Mime.MediaTypeNames.Application.Octet, name); файл будет загружен ... (я использовал его в mvc4, и он работает в прямом эфире также ..)

As например,

var audio_path = "/" + path.Substring(path.IndexOf("Content")); 
String audio_name = path.Substring(path.LastIndexOf("/") + 1); 
return File(audio_path, System.Net.Mime.MediaTypeNames.Application.Octet, audio_name); 
-1

можно использовать следующий код для загрузки файлов:

#region Download File ==> 
     public ActionResult downloadfile(string Filename, string MIMEType) 
     { 
      try 
      { 
       string file_name = "/Files/EvidenceUploads/" + Filename; 
       string contentType = ""; 
       //Get the physical path to the file. 
       string FilePath = Server.MapPath(file_name); 

       string fileExt = Path.GetExtension(file_name); 

       contentType = MIMEType; 

       //Set the appropriate ContentType. 
       Response.ContentType = contentType; 
       Response.AppendHeader("content-disposition", "attachment; filename=" + (new FileInfo(file_name)).Name); 

       //Write the file directly to the HTTP content output stream. 
       Response.WriteFile(FilePath); 
       Response.End(); 
       return View(FilePath); 
      } 
      catch 
      { 
       Response.End(); 
       return View(); 
       //To Do 
      } 

     } 
     #endregion 
+0

Still Это страница с указанием ошибки, пожалуйста, проверьте ошибку на pic http://s32.postimg.org/unacqjlz9/Screen_Shot_2016_04_26_at_7_44_24_PM.png –

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