【问题标题】:FileNotFoundException on IIS7 with ASP.NET Webform带有 ASP.NET Webform 的 IIS7 上的 FileNotFoundException
【发布时间】:2012-12-22 04:53:08
【问题描述】:

过去几个小时我确实经常使用 google 和 stackoverflow,但我没有找到帮助我解决问题的解决方案或主题。

任务和问题:

我创建了一个 ASP.NET Webform,它可以在我的本地机器上完美运行。它允许用户在 Dynamics CRM 2011 中创建客户。在此旁边,用户可以向客户添加注释并添加要上传的文件。

下一步是在 IIS7 上发布此 Webform,以便您可以在远程 Windows 2008 R2 服务器上远程访问它。如果我现在测试 Webform 并从 FileUpload 控件中选择一个数据,IIS7 没有得到正确的路径(FileNotFoundException),它总是使用他的根目录而不是数据中的文件路径,即使我想直接“上传”一个文件从安装 IIS 的服务器。

堆栈跟踪:

[FileNotFoundException: Die Datei "c:\windows\system32\inetsrv\test.txt" konnte nicht gefunden werden.]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +12898679
System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath) +2481
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +229
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +102
System.IO.File.OpenRead(String path) +55
FirstWebform._Default.create_Button_Click(Object sender, EventArgs e) in C:\Users\flah\Documents\Visual Studio 2010\Projects\FirstWebform\FirstWebform\Main.aspx.cs:86
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3707

我尝试过的:

  • 我已经读到我必须将 IIS7 上的文件保存在带有 FileUpload.Saveas("C:\\folder\\" + FileUpload.Filename) 的临时目录中,然后才能使用它 但同样的问题发生了,文件路径错误,它没有得到它。

  • 我尝试通过Path.GetFullPath(FileUploadControl.PostedFile.FileName); 获取路径名,但也没有用 - 与Server.MapPath(); 相同

  • 我已经为服务器上的用户设置了权限

我不明白为什么 IIS 总是将他的根目录用于 FileUpload,以及为什么它在我的本地机器上运行良好。

没有我尝试的重要代码:

if (FileUpload1.HasFile)
{
    FileStream stream = File.OpenRead(FileUpload1.PostedFile.FileName);
    byte[] byteData = new byte[stream.Length];
    stream.Read(byteData, 0, byteData.Length);
    stream.Close();

    //Dynamics CRM 2011 upload
    string encodedData = System.Convert.ToBase64String(byteData);                     
    newAnnotation.DocumentBody = encodedData;
    EntityReference refNote = new EntityReference();
    refNote.LogicalName = "account";
    refNote.Id = newAccountId;
    newAnnotation.Attributes.Add("objectid", refNote);
    service.Create(newAnnotation);
} else {
    EntityReference refNote = new EntityReference();
    refNote.LogicalName = "account";
    refNote.Id = newAccountId;
    newAnnotation.Attributes.Add("objectid", refNote);
    service.Create(newAnnotation);
}

我希望有人可以帮助我解决这个问题。也许它只是一个小配置或者我对整个架构的轻描淡写一定是错误的。

【问题讨论】:

    标签: c# asp.net file-upload iis-7 dynamics-crm-2011


    【解决方案1】:

    我相信您需要先处理该文件,然后才能使用它。 IIS 已收到文件,并将其保存在一个临时位置,直到FileUpload1 的用户代码处理保存。

    我相信您已经尝试过下面的代码,但只是为了确保;因为您需要在访问之前保存文件。这是执行此操作的标准方式,但您的代码示例不会尝试保存。

    尝试以下方法:

    String savePath = @"c:\temp\uploads\"; // Save folder - Ensure exists, permissions etc
    
    if (FileUpload1.HasFile)
    {
        // Append the name of the file to upload to the path.
        savePath += FileUpload1.FileName;
    
        // TODO : Error checking - verify file is accepted type/size/does not already exist
        FileUpload1.SaveAs(savePath);
    
        int fileLen = FileUpload1.PostedFile.ContentLength;
        Byte[] Input = new Byte[fileLen];
        System.IO.Stream myStream = FileUpload1.FileContent;
        myStream.Read(Input, 0, fileLen);
    
        // TODO : Other Processing
    
    } else {
        // TODO : Handle not hasfile
    }
    

    在页面底部查看FileUpload on MSDN Library 的一些示例。

    【讨论】:

    • 非常感谢。这两个答案都帮助我解决了问题。第一步是在处理对象之前声明SavePath,第二步是“var stream = FileUpload1.PostedFile.InputStream;”线-它一起解决了我的问题:)。也谢谢你的解释,让我大开眼界!问题已解决。
    • @Flo FileUpload1.FileContent 还应该为您提供底层流。但很高兴看到它起作用。
    【解决方案2】:

    您需要使用FileUpload.PostedFile.InputStream 来获取实际上传的内容。例如,

    if (FileUpload1.HasFile)
    {
        var stream = FileUpload1.PostedFile.InputStream;
        byte[] byteData = new byte[FileUpload1.PostedFile.ContentLength];
        stream.Read(byteData, 0, byteData.Length);
        stream.Close();
    
        ...
    

    编辑:关于您在现有代码中遇到的异常,问题出在File.OpenRead(FileUpload1.PostedFile.FileName); 行 - 您试图在服务器机器上打开一个具有名称的文件,而文件存在于客户端机器上。因为您没有提到任何路径信息,所以它正在某个默认(比如 system32)目录中查找文件 - 所以如果客户端机器和服务器机器相同并且文件是从根目录上传的,那么代码将起作用,但出于所有实际目的没用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-19
      相关资源
      最近更新 更多