C# 使用ASP.NET Webform的IIS7上的FileNotFoundException

C# 使用ASP.NET Webform的IIS7上的FileNotFoundException,c#,asp.net,file-upload,iis-7,dynamics-crm-2011,C#,Asp.net,File Upload,Iis 7,Dynamics Crm 2011,在过去的几个小时里,我确实经常使用google和stackoverflow,但我没有找到一个解决方案或主题来帮助我解决问题 任务和问题: 我创建了一个ASP.NET网络表单,它可以在我的本地maschine上完美运行。它允许用户在Dynamics CRM 2011中创建客户。接下来,用户可以向客户添加注释并添加要上载的文件 下一步是在IIS7上发布此Web表单,以便您可以远程访问它,它位于远程Windows 2008 R2服务器上。如果我现在测试Webform并从FileUpload控件中选择一

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

任务和问题:

我创建了一个ASP.NET网络表单,它可以在我的本地maschine上完美运行。它允许用户在Dynamics CRM 2011中创建客户。接下来,用户可以向客户添加注释并添加要上载的文件

下一步是在IIS7上发布此Web表单,以便您可以远程访问它,它位于远程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
我所尝试的:

  • 我已经了解到,在使用之前,我必须使用
    FileUpload.Saveas(“C:\\folder\\”+FileUpload.Filename)
    将文件保存在IIS7的临时目录中 但同样的问题也会发生,文件路径错误,并且无法获取

  • 我试图通过
    Path.GetFullPath(FileUploadControl.PostedFile.FileName)获取路径名也不起作用-与
    Server.MapPath()相同

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

我不明白为什么IIS总是使用他的根目录进行
文件上传
,为什么它在我的本地机器上运行良好

没有我尝试的重要代码:

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
}
请参阅页面底部的一些示例以了解。

您需要使用以获取实际上载的内容。比如说,

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

    ...

EDIT:关于现有代码的异常,问题在于
File.OpenRead(FileUpload1.PostedFile.FileName)行-当客户端计算机上存在文件时,您试图在服务器计算机上打开一个名为的文件。因为您没有提到任何路径信息,所以它正在某个默认(比如system32)目录中查找文件-因此,如果客户端计算机和服务器计算机相同,并且文件是从根目录上载的,那么代码可以工作,但对于所有实际用途来说,它都是无用的。

非常感谢您。这两个答案都帮助我解决了这个问题。第一步是在处理对象之前声明保存路径,第二步是“var stream=FileUpload1.PostedFile.InputStream;”行-它一起解决了我的问题:)。也谢谢你的解释,它打开了我的心扉!问题已解决。@Flo
FileUpload1.FileContent
还应提供基础流。但很高兴看到它起作用。