2010-02-24 3 views
2

이것이 첫 번째 질문입니다. 이 사이트에 기여한 모든 분들께 감사 드리며, 프로그래밍을 가르치 려 할 때 커다란 도움이되었습니다.업로드 한 이미지를 저장하는 중 문제가 발생했습니다.

업로드 된 이미지를 저장하는 데 문제가 있습니다. image.save 함수로 뭔가 잘못하고 있다고 생각합니다. 다음과 같이 내 코드는 다음과 같습니다

컨트롤러 :

[AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult Index(string fileName, HttpPostedFileBase pic) 
    { 
     string mimeType = pic.ContentType; 
     byte[] imagedata = new byte[pic.ContentLength]; 
     pic.InputStream.Read(imagedata, 0, pic.ContentLength); 
     Image outfitImage = (Image.FromStream(new MemoryStream(imagedata))); 
     string filePath = @"c:\test.jpg"; 
     outfitImage.Save(filePath); 
     return View(); 
    } 

보기 :

<% using (Html.BeginForm("Index","Test" ,FormMethod.Post,new {enctype="multipart/form-data"})) {%> 

    <fieldset> 
     <legend>Fields</legend> 
    <p> 
      <label for="fileName">Name:</label> 
      <%= Html.TextBox("fileName") %> 
    </p> 
    <p> 
     Upload new image: <input type="file" name="pic" /> 
    </p> 
    <p> 
      <input type="submit" value="Create" /> 
    </p> 
    </fieldset> 
<% } %> 

스택 추적은 다음과 같다 :

Server Error in '/' Application. 
A generic error occurred in GDI+. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. 

Source Error: 

Line 51:    
Line 52:    string filePath = @"c:\test.jpg"; 
Line 53:    outfitImage.Save(filePath); 
Line 54: 
Line 55:    return View(); 


Source File: C:\Users\Solomon\SolomonApp\AmIStylin\AmIStylin\Controllers\TestController.cs Line: 53 

Stack Trace: 

[ExternalException (0x80004005): A generic error occurred in GDI+.] 
    System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) +377630 
    System.Drawing.Image.Save(String filename, ImageFormat format) +69 
    System.Drawing.Image.Save(String filename) +25 
    AmIStylin.Controllers.TestController.Index(String fileName, HttpPostedFileBase pic) in C:\Users\Solomon\SolomonApp\AmIStylin\AmIStylin\Controllers\TestController.cs:53 
    lambda_method(ExecutionScope , ControllerBase , Object[]) +205 
    System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17 
    System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24 
    System.Web.Mvc.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7() +52 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +254 
    System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +19 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +192 
    System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +399 
    System.Web.Mvc.Controller.ExecuteCore() +126 
    System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +27 
    System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7 
    System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +151 
    System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +57 
    System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75 


Version Information: Microsoft .NET Framework Version:2.0.50727.4062; ASP.NET Version:2.0.50727.3074 
도움을 너무 많이

감사합니다!

+0

루프에서'pic.InputStream.Read'를 호출해야합니다. 모든 것을 한 번에 읽을 수는 없으므로 루프에서 호출해야합니다. (반환 값을 확인하십시오) – SLaks

+0

감사 SLaks. 내가 정확히 어떻게 할 수 있니? – Solomon

답변

2

이미지를 JPEG로 변환하려고하십니까?

그렇지 않은 경우 pic.Save(@"C:\text.jpg")으로 전화하면됩니다.

ASP.Net에 C : 드라이브의 루트에 대한 쓰기 권한이 없기 때문에 오류가 발생할 수 있습니다. (GDI +는 재미있는 오류를 줄 수 있습니다.)
이 코드는 어떻게 그리고 어디에서 실행됩니까? (카시니? IIS)

+0

고마워! 파일 경로를 하드 코딩하려고했지만 같은 오류가 발생했습니다. 코드는 로컬 컴퓨터 (Visual Studio의 오래된 F5)에서 실행되므로 IIS라고 생각합니다. 사진을 JPEG로하고 싶습니다. – Solomon

+0

VS에서 F5를 누르면 Cassini가 실행 중입니다. – SLaks

+0

'pic.Save (@ "C : \ text.jpg")'로 변경하여 오류가 있는지 확인하십시오. – SLaks

1

난 당신이, 웹 서버는 상대적으로 저장하는 파일 서버에 그 번역 할 위치 만 파악하고 저장한다.

string relativePath = "~/somefolder/somefile.ext"; 
string diskPath = ControllerContext.HttpContext.Server.MapPath(newFileWebPath); 
pic.SaveAs(diskPath); 
+0

jpeg로 변환하지 않는 한. – SLaks

+0

감사합니다. 내 호스팅 제공 업체 (랙 스페이스)는 Medium Trust이며 배포 할 때 상대 경로가 아닌 절대 경로를 사용해야합니다. – Solomon

+0

'Server.MapPath'는 절대 경로를 반환합니다. 이 코드는 경로에 대한 쓰기 권한이 있다고 가정하고 작동해야합니다. (당신은 아마하지 않습니다) – SLaks

관련 문제