在C#中获取映像到服务器的路径

时间:2013-07-30 13:34:59

标签: c# asp.net-mvc azure epplus

我正在使用EPPlus(开发人员为一个很棒的库提供道具)制作一个将在浏览器中下载的Excel文档。在生成Excel文档的代码中,我有几行要在Excel文档中包含图像文件。

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)

引用此图像文件以使其适用于各种环境的最佳方法是什么?

2 个答案:

答案 0 :(得分:0)

异常中的给定路径是该文件的正确路径?如果是权利?如果没有,则例外是正确的。 :)

答案 1 :(得分:0)

我在部署azure时遇到了同样的问题。 Server.MapPath在azure

中不起作用

我解决了这个问题。但我忘记了如何解决这个问题。

请尝试这种代码方式。我想我用这个代码来解决你的问题

// 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