2013-07-30 102 views
0

我正在使用EPPlus(道具爲開發人員提供一個極好的庫)來創建將在瀏覽器中下載的Excel文檔。在生成Excel文檔的代碼中,我有幾行是爲了在Excel文檔中包含圖像文件。在C#中獲取服務器路徑圖像

Image logoImg = Image.FromFile(Server.MapPath("~/Images/bill_logo.png")); 
var logo = ws.Drawings.AddPicture("Logo", logoImg); 

當我在我的機器上本地運行項目時,圖像按預期顯示。然而,當我部署到我們在Azure的測試環境中,我得到以下錯誤:

System.IO.FileNotFoundException: C:\DWASFiles\Sites\sitename\VirtualDirectory0\site\wwwroot\Images\bill_logo.png 
at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) 
at System.Drawing.Image.FromFile(String filename) 
at emergePortal.Web.Controllers.BillingController.buildWorksheet(ExcelWorksheet ws, String sheetName, IEnumerable`1 lineItems) in c:\a\src\emergePortal\emergePortal.Web\Controllers\BillingController.cs:line 202 
at emergePortal.Web.Controllers.BillingController.getBill(Int32 billMonth, Int32 billYear) in c:\a\src\emergePortal\emergePortal.Web\Controllers\BillingController.cs:line 106 
at lambda_method(Closure , ControllerBase , Object[]) 
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) 
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) 
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) 
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() 
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) 
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End() 
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) 
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() 
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() 
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) 
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End() 
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) 
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<>c__DisplayClass2a.<BeginInvokeAction>b__20() 
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) 

什麼是引用該圖像文件,以便它可以跨環境的最佳途徑?

+0

設置讀取和寫入您的圖片文件夾 – 2013-07-30 13:38:43

+0

我可能是錯的,止跌」這是第一次,但我不相信你可以在Azure中編輯權限。 – Trevor

+0

您可以將票發送到Azure。 http://www.windowsazure.com/en-us/support/options/ – 2013-07-30 14:38:31

回答

0

異常中的給定路徑是該文件的正確路徑?如果是權利?如果不是,則例外是正確的。 :)

+0

我不確定它是否是正確的文件路徑。我無法瞭解Azure中項目的文件結構。我會檢查文件的權限 - 我假設我只需要讀取權限? – Trevor

+0

經過一番搜索後,我不相信你可以在Azure中更改文件權限。有人能糾正我嗎? http://stackoverflow.com/questions/11016098/folder-permissions-in-azure-web-sites – Trevor

0

我在部署天藍色時遇到了同樣的問題。該是使用Server.Mappath不蔚藍

工作我解決了這個問題。但我忘了如何解決這個problem.sorry

請嘗試這種編碼方式。我覺得我用這個代碼,您的問題

// Retrieve an object that points to the local storage resource 
LocalResource localResource = RoleEnvironment.GetLocalResource("localStoreTwo"); 

//Define the file name and path 
string[] paths = { localResource.RootPath, "MyStorageTest.txt"}; 
String filePath = Path.Combine(paths); 

更多詳細信息請參閱本

Getting Your Physical Application Path in Window Azure

Msdn