2010-12-06 58 views
2

我試圖將圖像上載到ftp。我需要將它放在由特定名稱調用的壓縮文件夾中,然後將該文件夾上傳到特定目錄。每次我嘗試時,我得到一個錯誤遠程服務器返回一個錯誤:(550)文件不可用 此代碼工作正常,當我試圖一次上傳一個圖像。這裏我試圖上傳整個文件夾。我檢查了uri(我從調試中複製了它),並且它很好。有沒有不同的方式,我必須做上傳文件夾?我認爲這是一個寫權限問題,但我可以手動登錄並將文件夾上傳到適當的位置。然後我試圖獲得我能夠的目錄列表。我無法將文件夾上傳到根目錄。我非常絕望!我甚至不知道在哪裏谷歌!將文件夾上傳到ftp子文件夾返回錯誤(550)文件不可用,無法訪問

string ftpPassword = ConfigurationManager.AppSettings["ftpPassword"].ToString(); 
    string uri = remoteDirectory; 
    FileInfo fileInf = new FileInfo(FileToUpload); 
    // Create FtpWebRequest object from the Uri provided 
    FtpWebRequest reqFTP = null; 
    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); 
    reqFTP.Credentials = new NetworkCredential(ftpUsername, ftpPassword); 
    reqFTP.KeepAlive = false; 
    reqFTP.Method = WebRequestMethods.Ftp.UploadFile; 
    // Specify the data transfer type. 
    reqFTP.UseBinary = true; 
    // Notify the server about the size of the uploaded file 
    reqFTP.ContentLength = fileInf.Length; 
    // The buffer size is set to 2kb 
    int buffLength = 2048; 
    byte[] buff = new byte[buffLength]; 
    int contentLen; 
    // open file to be uploaded 
    using (FileStream fs = fileInf.OpenRead()) 
    { 
    try 
    { 
    // Stream to which the file to be upload is written 
    using (Stream strm = reqFTP.GetRequestStream()) 
    { 
    // Read from the file stream 2kb at a time till Stream content ends 
    contentLen = fs.Read(buff, 0, buffLength); 
    while (contentLen != 0) 
    { 
    // Write Content from the file stream to the FTP Upload Stream 
    strm.Write(buff, 0, contentLen); 
    contentLen = fs.Read(buff, 0, buffLength); 
    } 
    } 
    reqFTP = null; 
    ////Update the database with the new image location and delete the img from the uploadedimages folder 
    //DataAccess.UpdateImageDB(item.ProductID, item.ImgFolder + "/" + item.IMG); 
    System.IO.File.Delete(fileInf.ToString()); 
    } 
    { 
    Console.WriteLine(ex.Message, "Upload Error"); 
    } 

回答

0

你可能需要檢查是否目錄(文件夾)是否存在,如果沒有的話,你需要在FTP創建它。我很確定大多數FTP客戶端都爲你做這件事。

+0

我知道該文件夾存在。我從數據庫中提取文件夾名稱。另外,當我瀏覽它時,我將uri複製並粘貼到瀏覽器中,並且沒有任何問題。 – SSs 2010-12-08 14:02:18

0

我不得不進入NTFS權限,並將「完全控制」IUSR添加到FTP文件夾。

0

爲了解決此問題,需要強制System.Net.FtpWebRequest命令恢復舊的行爲,如何使用.Net Framework 2.0/3.5中的舊行爲併發出額外的CWD命令發佈實際的命令。

爲了做到這一點,需要在調用System.Net.FtpWebRequest類的任何實例之前放置以下代碼。下面的代碼只需要調用一次,因爲它改變了整個應用程序域的設置。

private static void SetMethodRequiresCWD() 
{ 
    Type requestType = typeof(FtpWebRequest); 
    FieldInfo methodInfoField = requestType.GetField("m_MethodInfo", BindingFlags.NonPublic | BindingFlags.Instance); 
    Type methodInfoType = methodInfoField.FieldType; 


    FieldInfo knownMethodsField = methodInfoType.GetField("KnownMethodInfo", BindingFlags.Static | BindingFlags.NonPublic); 
    Array knownMethodsArray = (Array)knownMethodsField.GetValue(null); 

    FieldInfo flagsField = methodInfoType.GetField("Flags", BindingFlags.NonPublic | BindingFlags.Instance); 

    int MustChangeWorkingDirectoryToPath = 0x100; 
    foreach (object knownMethod in knownMethodsArray) 
    { 
     int flags = (int)flagsField.GetValue(knownMethod); 
     flags |= MustChangeWorkingDirectoryToPath; 
     flagsField.SetValue(knownMethod, flags); 
    } 
} 

http://support.microsoft.com/kb/2134299

相關問題