我真的很多過去幾個小時使用谷歌和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);
}
我希望有人能幫助我與此有關。也許它只是一個小配置,或者我對整個架構的輕描淡寫都是錯誤的。
非常感謝。兩個答案都幫助我解決了這個問題。第一步是在處理對象之前聲明SavePath,第二步是「var stream = FileUpload1.PostedFile.InputStream;」行 - 一起它只是管理我的問題:)。 感謝您的解釋,它打開了我的腦海! 問題已解決。 – Flo
@Flo'FileUpload1.FileContent'還應該爲您提供基礎流。但很高興看到它的工作。 – Kami