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

本文关键字:服务器 路径 映像 获取 | 更新日期: 2023-09-27 18:25:27

我正在使用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)

引用此图像文件以使其跨环境工作的最佳方式是什么?

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

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

我在部署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); 

更多详细信息,请参阅此

在Window Azure 中获取物理应用程序路径

和Msdn