2010-02-24 111 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 2010-02-24 04:17:45

+0

謝謝SLaks。我到底該怎麼做? – Solomon 2010-02-24 16:24:25

回答

2

你想轉換圖像爲JPEG?

如果不是,則應該致電pic.Save(@"C:\text.jpg")

您可能會遇到錯誤,因爲ASP.Net不具有對C:驅動器根目錄的寫入權限。 (GDI +可以給出有趣的錯誤)
這段代碼如何以及在哪裏運行? (卡西尼?IIS?)

+0

謝謝!我試圖硬編碼的文件路徑,但得到了同樣的錯誤。代碼正在我的本地機器上運行(Visual Studio中的舊F5),所以我認爲它是IIS。 我希望圖片是JPEG。 – Solomon 2010-02-24 16:18:43

+0

如果你在VS中按F5,你正在運行卡西尼。 – SLaks 2010-02-24 16:19:39

+0

嘗試將其更改爲'pic.Save(@「C:\ text.jpg」)'並查看它是否給出錯誤。 – SLaks 2010-02-24 16:21:21

1

我只想找出你想要保存它相對於Web服務器,那意思就是文件服務器,然後將其保存。

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

除非他想將其轉換爲jpeg。 – SLaks 2010-02-24 14:01:44

+0

謝謝!我的託管服務提供商(rackspace)是中等信任,我需要使用絕對路徑,而不是相對路徑,儘管當我部署時... – Solomon 2010-02-24 16:11:39

+0

'Server.MapPath'返回一個絕對路徑。假設你有對路徑的寫入權限,這段代碼應該可以工作。 (你可能不這樣做) – SLaks 2010-02-24 16:20:48