2014-11-12 2 views
1

내 응용 프로그램에 음악 업로드를하고 있는데, uploadify 플러그인을 사용하면 업로드가 작동하지만 경로 참조 (음악이 내 프로젝트에 저장되는 위치)를 저장하면 null 값을 얻게됩니다. , 내가 어떻게 이럴 수 있니?데이터베이스에서 파일 경로를 저장하는 방법은 무엇입니까?

업로드 방법은 MusicController에서

public ActionResult Upload() 
{ 
    var file = Request.Files["Filedata"]; 
    string savePath = Server.MapPath(@"~\mp3\" + file.FileName); 
    file.SaveAs(savePath); 
    return Content(Url.Content(@"~\mp3\" + file.FileName)); 
} 

이 경우에 MusicController

public ActionResult Create(Musica musica) 
     { 
      MembershipUser user = Membership.GetUser(); 
      int userId = Int32.Parse(Membership.GetUser().ProviderUserKey.ToString()); 
      string userName = user.UserName; 

      musica.UserId = userId; 
      musica.NomeArtista = userName; 

      if (musica.isFree) 
      { 

       musica.Preco = 0; 
      } 

      //Here I try to take the path value 
      musica.path = Upload().ToString(); 

      if (ModelState.IsValid) 
      { 
       db.Musicas.Add(musica); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      ViewBag.GeneroId = new SelectList(db.Generos, "GeneroId", "Nome", musica.GeneroId); 
      return View(musica); 
     } 

에서 방법을 만들고, 난 단지 내 데이터베이스에 저장하려면,이 정보 :

~\mp3\MusicExample.mp3 

누군가 나를 도울 수 있습니까?

+0

이'~ \ mp3 \ MusicExample.mps'를 데이터베이스의'varchar' 필드로 저장하려고합니까, 아니면 [BLOB] (http://stackoverflow.com/ 질문/23894716/how-to-save-image-as-varbinay-in-sql-server-2008r2/23899574 # 23899574)? – alykins

+0

'Create' 액션에서'Upload' 컨트롤러 액션을 호출하는 것처럼 보입니다. 그건 말이 안되요. 'Upload' 메쏘드의 유일한 사용이 파일을 저장하는 것이라면 (즉, 전혀 액션이 아닙니다)'ActionResult'가 아닌'string' 또는 그와 비슷한 것을 반환해야합니다. –

+0

@alykins varchar로! –

답변

2

편집 : 그래서 사용중인 업로드 방법은 비동기식이며 파일을 업로드하고 파일 경로를 다시 페이지로 반환합니다. 원하는 파일 경로를 반환하는 업로드 방법을 변경 jQuery를 그것을 포착하고이 업로드 된 후 정보를 보유하고 컨트롤을 만들 :

업로드 방법이 있어야한다 :에

public ActionResult Upload() 
{ 
    var file = Request.Files["Filedata"]; 
    string savePath = Server.MapPath(@"~\mp3\" + file.FileName); 
    file.SaveAs(savePath); 
    return Content(@"~\mp3\" + file.FileName); 
} 

Somwhere 당신의 , 자바 스크립트에서

@Html.EditorFor(model => model.path, new { id = "path"}) 

를 반환 값을 캡처하고 무지카의 path 속성에 대한 새로운 텍스트 상자 내부를 설정 :보기는 path 속성에 대한 컨트롤을 추가

작성 방법은
'onUploadSuccess': function (file, data, response) { 
       $("#path").val(data); 
      } 

, Upload를 호출하는 코드를 제거 :

//Stuff above here 
if (musica.isFree) 
{ 
    musica.Preco = 0; 
} 

//Here I try to take the path value 
//musica.path = Upload().ToString(); 

if (ModelState.IsValid) 
//stuff below here 

를 이제, 당신은 생성 버튼을 클릭하면 자동으로 파일 경로의 경로 속성을 설정해야하고, 그것을해야 just work

+0

작동하지 않습니다. 오류가 표시됩니다. fileName 행에 "Unrecognized escape sequence"가 표시됩니다. –

+0

@PedroHenrique 업데이트 됨. 백 슬래쉬를 이스케이프 처리하기 위해 또 다른'\'를 추가하십시오. –

+0

@DaveZich 아직 일하지 않고있다. 왜냐하면 내가 'musica'라고 부를 때가 그 때문이라고 생각한다.path = upload()'응용 프로그램이 메서드를 다시 실행하고 nullpointerException이 표시됩니다. –

관련 문제