简体   繁体   中英

Unable to view Local Drive Files in published Website

I want to view files in local drive in computer. The solution worked on my local computer but when i published website it doesn't work.I have listed files successfully but when i try to download file. it gives error. It worked perfectly on my Local pc but when i published it on IIS and try to view file it gives error.

    Response.ContentType = "text/plain";
    Response.AddHeader("Content-Disposition", "inline;filename=" + fileName);
    Response.TransmitFile(Server.MapPath(Path.Combine("~/E:/NewFolder/", fileName)));
    Response.End();

Error:

The given path's format is not supported. 
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.NotSupportedException: The given path's format is not supported.

Source Error: 


Line 28:         Response.ContentType = "text/plain";
Line 29:         Response.AddHeader("Content-Disposition", "inline;filename=" + fileName);
Line 30:         Response.TransmitFile(Server.MapPath(Path.Combine("~/E:/NewC/", fileName)));
Line 31:         Response.End();
Line 32:         }


Source File: E:\New folder\ViewFiles\ViewFiles\Default2.aspx.cs    Line: 30 

Stack Trace: 


[NotSupportedException: The given path's format is not supported.]
   System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath) +12651960
   System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath) +165
   System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList) +112
   System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList) +38
   System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String path) +92
   System.Web.HttpRequest.MapPath(VirtualPath virtualPath, VirtualPath baseVirtualDir, Boolean allowCrossAppMapping) +549
   System.Web.HttpServerUtility.MapPath(String path) +234
   ViewFiles.Default2.Page_Load(Object sender, EventArgs e) in E:\New folder\ViewFiles\ViewFiles\Default2.aspx.cs:30
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
   System.Web.UI.Control.LoadRecursive() +71
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3064

You are giving the physical path of file in Server.MapPath whereas you have to give the virtual path. This is what MSDN says about Server.MapPath Specifies the relative or virtual path to map to a physical direct

If the path is outside the hosted website you can make virtual directory of the folder, containing the files you want to transfer, inside the website you have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM