2012-12-01 3 views
1

내가 업로드하고있어 이미지 :자원을 찾을 수없는 이유는

<img height="100px" src="/Content/Uploads/Thumb/833c4384-884d-4250-982c-d5df0fa875ef.jpg" width="100px"/> 

하지만 돈 :

[HttpPost] 
public ActionResult Create(PlaceViewModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     string fileName = Guid.NewGuid().ToString() + ".jpg"; 
     string serverPath = Server.MapPath("~"); 
     string imagesPath = serverPath + "Content\\Uploads\\"; 
     string thumbPath = imagesPath + "Thumb\\"; 
     string fullPath = imagesPath + "Full\\"; 
     ImageModel.ResizeAndSave(thumbPath, fileName, model.ImageUploaded.InputStream, 100, true); 
     ImageModel.ResizeAndSave(fullPath, fileName, model.ImageUploaded.InputStream, 600, true); 

     model.Image = fileName; 

     var place = new Place(); 
     model.ConvertToData(place); 

     _placeRepository.Add(place); 
     _placeRepository.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    return View(model); 
} 

파일 업로드 및

나는 HTML에서 이미지를 호출하고있어 디스크에 물리적 존재 이 이미지가 보이지 않습니다. 내가 localhost:23354/Content/Uploads/Thumb/833c4384-884d-4250-982c-d5df0fa875ef.jpg를 호출하면

내가 가진 오류 : 내가 잘못

The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Content/Uploads/Thumb/833c4384-884d-4250-982c-d5df0fa875ef.jpg

을 뭐하는 거지?

+6

이 URL의 루트에있는 내용입니다 filename.jpg.jpeg 것을 밝혀 '.JPG'이름 확장명을 추가?/content에서 이미지를 요청했지만 업로드했을 때 ~/content /에 업로드했습니다 ... 같은 위치입니까? – Brettski

+0

왜이 질문에 두 차례 투표가 있습니까? – Brettski

+0

Brettski에 동의합니다. 그것은 아주 유효한 질문입니다. +1. –

답변

0

패자.

이미지 ImageModel.ResizeAndSave에 저장

newImage.Save(savePath + fileName + ".jpg", ImageFormat.Jpeg); 

이 파일이

2

가상 디렉터리에서 IIS를 실행하는 경우 가상 디렉터리를 먼저 지정해야합니다. 귀하의 견해에 하드 코드를해서는 안됩니다. 가상 디렉토리없이 로컬로 실행되는 경우 도우미가 생성 이제

<img height="100px" src="@Url.Content("~/Content/Uploads/Thumb/833c4384-884d-4250-982c-d5df0fa875ef.jpg")" width="100px"/> 

: 항상 헬퍼를 사용

/Content/Uploads/Thumb/833c4384-884d-4250-982c-d5df0fa875ef.jpg 

을 그리고 가상 디렉토리 내에 IIS에서 응용 프로그램을 업로드 할 때 도우미 올바른을 생성합니다 다시 한번 url :

/AppName/Content/Uploads/Thumb/833c4384-884d-4250-982c-d5df0fa875ef.jpg 

이렇게 보이는대로 URL을 하드 코딩해서는 안됩니다. 같은 자바 스크립트 파일에있는 URL을 의미합니다. ASP.NET MVC 애플리케이션에서 URL을 처리 할 때 항상 헬퍼를 사용하십시오.

+0

이 도움이되지 않았습니다. – Mediator

+0

여전히 내 대답과 같이 도우미를 사용해야합니다. 그렇지 않으면 IIS에 배포 할 때 코드가 손상됩니다. –

+0

예 예 알아요. 감사 – Mediator

관련 문제