업로드 비트를 성공적으로 완료했습니다-내 SQL Server에 MP3 파일을 업로드했지만 파일 다운로드에 도움이 필요합니다 .... 다음 코드를 사용하여 MP3 파일을 업로드합니다.
public ActionResult Create([Bind(Include = "ID,Artist,Album")] TrackUpload trackUpload, HttpPostedFileBase upload)
{
try
{
if (ModelState.IsValid)
{
if (upload != null && upload.ContentLength > 0)
{
var song = new File
{
FileName = System.IO.Path.GetFileName(upload.FileName),
FileType = FileType.Songs,
ContentType = upload.ContentType
};
using (var reader = new System.IO.BinaryReader(upload.InputStream))
{
song.Content = reader.ReadBytes(upload.ContentLength);
}
trackUpload.Files = new List<File> { song };
}
db.TrackUploads.Add(trackUpload);
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (RetryLimitExceededException /* dex */)
{
ModelState.AddModelError("", "Cant save changes.");
}
return View(trackUpload);
}
파일이 데이터베이스에 저장됨을 의미하는 잘 작동합니다. 이제 엔티티 프레임 워크를 사용하여 SQL DB에서 MP3 파일을 어떻게 다운로드합니까?
MVC 컨트롤러에서 MP3 파일을 다운로드하려면 MIME 유형을 , 또는 (자세한 내용은 http://filext.com/file-extension/MP3 참조) FileContentResult
로 설정했는지 확인하십시오 .audio/mp3
audio/mpeg3
audio/x-mpeg3
다음은 파일 다운로드 시나리오를 정의하는 간단한 컨트롤러 코드입니다.
/*
* assume Id becomes file ID key to download
* this code provides simple download function by returning FileContentResult with proper MIME type
*/
public FileContentResult DownloadFile(int Id)
{
// assume FileContext is your file list DB context with FileName property (file name + extension, e.g. Sample.mp3)
using (FileContext db = new FileContext())
{
var download = db.TrackUploads.Where(x => x.ID == Id).SingleOrDefault();
if (download != null)
{
// remove this line if you want file download on the same page
Response.AddHeader("content-disposition", "inline; filename=" + download.FileName);
return File(download.FileName, "audio/mp3");
}
else
{
return null;
}
}
}
추가
다운로드하기 전에 파일 확장자에서 MIME 유형을 얻으려면 다음 코드 부분이 작동 할 수 있습니다.
if (download != null)
{
String mimeType = MimeMapping.GetMimeMapping(download.FileName);
...
// other tasks
...
return File(download.FileName, mimeType);
}
참조 : Entity FrameWork를 사용하여 SQL DB에서 파일보기 및 다운로드 (MP3 확장에 대한 조정 포함)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다