2013-01-08 42 views
1

我真的很多過去幾個小時使用谷歌和stackoverflow,但我沒有找到一個解決方案或主題,這幫助我解決了我的問題。IIS7與ASP.NET Webform的FileNotFoundException

的任務和問題:

我創建了完全運行在我的本地maschine一個ASP.NET網絡表單。它允許用戶在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); 
} 

我希望有人能幫助我與此有關。也許它只是一個小配置,或者我對整個架構的輕描淡寫都是錯誤的。

回答

0

我相信你需要處理文件纔可以使用。 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

+0

非常感謝。兩個答案都幫助我解決了這個問題。第一步是在處理對象之前聲明SavePath,第二步是「var stream = FileUpload1.PostedFile.InputStream;」行 - 一起它只是管理我的問題:)。 感謝您的解釋,它打開了我的腦海! 問題已解決。 – Flo

+0

@Flo'FileUpload1.FileContent'還應該爲您提供基礎流。但很高興看到它的工作。 – Kami

0

您需要使用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)目錄中的文件 - 所以如果客戶機和服務器機器是相同的,並且文件是從根目錄上傳的,那麼代碼將起作用,但是出於所有實際目的它是無用的。