繁体   English   中英

使用ASP.NET Webform的IIS7上的FileNotFoundException

[英]FileNotFoundException on IIS7 with ASP.NET Webform

在过去的几个小时中,我确实大量使用了Google和stackoverflow,但是我没有找到能够帮助我解决问题的解决方案或主题。

任务和问题:

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

下一步是在IIS7上发布此Webform,以便可以在远程Windows 2008 R2 Server上远程访问它。 如果我现在测试Webform并从FileUpload控件中选择数据,则IIS7无法获取正确的Path (FileNotFoundException) ,即使我想直接“上传”文件,它也始终使用其根目录而不是数据中的Filepath。从安装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

我试过的

  • 我已经读到我必须先使用FileUpload.Saveas("C:\\\\folder\\\\" + FileUpload.Filename)将文件保存在IIS 7的临时目录上,然后才能使用,但是出现相同的问题,文件路径错误它没有事件得到它。

  • 我试图通过Path.GetFullPath(FileUploadControl.PostedFile.FileName);获取路径名Path.GetFullPath(FileUploadControl.PostedFile.FileName); 两者都不起作用-与Server.MapPath();相同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);
}

我希望有人可以帮助我。 也许只是一点点配置,或者我对整个体系结构的轻描淡写肯定是错误的。

我相信您需要先处理该文件,然后才能使用它。 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
}

请参阅页面底部的一些示例,以获取MSDN Library上的FileUpload

您需要使用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)中查找文件-因此,如果客户端计算机和服务器计算机相同,并且文件是从根目录上载的,则该代码可以工作,但出于所有实际目的这是没有用的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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