从服务器下载文件以供以guest身份登录的用户使用

时间:2010-02-05 15:58:43

标签: c# asp.net download forms-authentication

我有一个代码,允许您从服务器下载任何文件到本地主机。

当我以管理员身份登录时,此代码工作正常,但当我以访客用户身份登录时,它不允许我下载..并且我收到错误消息说...

  

外部组件抛出异常。   描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

 Exception Details: System.Runtime.InteropServices.SEHException: External component has thrown an exception.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

我的代码如下

string filepath = restoredFilename.ToString();

            // Create New instance of FileInfo class to get the properties of the file being downloaded
            FileInfo myfile = new FileInfo(filepath);

            // Checking if file exists
            if (myfile.Exists)
            {

                // Clear the content of the response
                Response.ClearContent();

                // Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
                Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);

                //Response.AddHeader("Content-Disposition", "inline; filename=" + myfile.Name);
                // Add the file size into the response header
                Response.AddHeader("Content-Length", myfile.Length.ToString());

                // Set the ContentType
                Response.ContentType = ReturnExtension(myfile.Extension.ToLower());

                //// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
                Response.TransmitFile(myfile.FullName);

                // End the response
                Response.End();
            }

我必须使用访客下载文件..请帮助谢谢

堆栈追踪:

[SEHException (0x80004005): External component has thrown an exception.]
   xyzgui.Guest.DriveContents.RestoreOneFileForGUI(Int32 machineID_Download, Int64 fileID, StringBuilder restoredFilename, UInt32 restoredFilenameBufsize) +0
   xyzgui.Guest.DriveContents.file_Click(Object sender, EventArgs e) in C:\Users\jagmit\Documents\Visual Studio 2008\Projects\xyzgui\xyzgui\Guest\DriveContents.aspx.cs:341
   System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +79
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

1 个答案:

答案 0 :(得分:0)

您使用的是哪种身份验证机制:表单还是Windows?

你是否开启了假扮?

我相信当您遇到安全问题时,通常会抛出您看到的错误 - 当您说“我以管理员身份登录时”时,这听起来很可能。

我们没有看到的方法RestoreOneFileForGUI还有更多 - 因为它看起来像是在.NET之外抛出安全异常 - 如果它像访客帐户一样简单(网站帐户)在无法访问文件路径的情况下运行,您会看到FileInfo constructor中的标准SecurityExceptionUnauthorizedException