2012-02-14 22 views
1

C#でFTPを使用してXMLファイルをアップロードする方法を教えてください。イムは、現在たFtpWebRequestメソッドを使用して、その私にエラーを与えてFTPでXMLファイルをアップロードする#

私のコードはここ

//Create FTP request 
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://www.itsthe1.com/profiles/nuwan/sample.txt"); 

request.Method = WebRequestMethods.Ftp.UploadFile; 
request.Credentials = new NetworkCredential(Username, Password); 
request.UsePassive = true; 
request.UseBinary = true; 
request.KeepAlive = false; 

//Load the file 
FileStream stream = File.OpenRead(@"C:\sample.txt"); 
byte[] buffer = new byte[stream.Length]; 

stream.Read(buffer, 0, buffer.Length); 
stream.Close(); 

//Upload file 
Stream reqStream = request.GetRequestStream(); 
reqStream.Write(buffer, 0, buffer.Length); 
reqStream.Close(); 
+0

どのようなエラーがありますか?コードを教えてもらえますか? – Sven

+0

コードはありません+エラーはありません= – Shai

+0

リモートサーバーからエラーが返されました:(553)ファイル名は許可されていません。ストリーム内のreqStream = request.GetRequestStream();行 – madbuddy

答えて

0

です(私はVB.NETでそれを書いたが、うまくいけば、あなたができるFTPを通じて任意のファイルをアップロードするために私の成功した作業のコードですここで

Public Function Upload(ByVal fi As FileInfo, Optional ByVal targetFilename As String = "") As Boolean 
    'copy the file specified to target file: target file can be full path or just filename (uses current dir) 
    '1. check target 
    Dim target As String 
    If targetFilename.Trim = "" Then 
     'Blank target: use source filename & current dir 
     target = GetCurrentUrl() & "/" & fi.Name 
    Else 
     'otherwise treat as filename only, use current directory 
     target = GetCurrentUrl() & "/" & targetFilename 
    End If 
    Dim URI As String = target 'GetCurrentUrl() & "/" & target 
    'perform copy 
    Dim ftp As Net.FtpWebRequest = GetRequest(URI) 
    'Set request to upload a file in binary 
    ftp.Method = Net.WebRequestMethods.Ftp.UploadFile 
    ftp.UseBinary = True 
    'Notify FTP of the expected size 
    ftp.ContentLength = fi.Length 
    'create byte array to store: ensure at least 1 byte! 
    Const BufferSize As Integer = 2048 
    Dim content(BufferSize - 1) As Byte, dataRead As Integer 
    'open file for reading 
    Using fs As FileStream = fi.OpenRead() 
     Try 
      'open request to send 
      Using rs As Stream = ftp.GetRequestStream 
       Dim totBytes As Long = 0 
       Do 
        dataRead = fs.Read(content, 0, BufferSize) 
        rs.Write(content, 0, dataRead) 
        totBytes += dataRead 
        RaiseEvent StatusChanged(totBytes.ToString() & " bytes sent...") 
       Loop Until dataRead < BufferSize 
       rs.Close() 
       RaiseEvent StatusChanged("File uploaded successfully") 
      End Using 
     Catch ex As Exception 
      RaiseEvent StatusChanged("Error: " & ex.Message) 
     Finally 
      'ensure file closed 
      fs.Close() 
     End Try 
    End Using 
    ftp = Nothing 
    Return True 
End Function 

)C#オンライン翻訳者のいずれかを使用してそれを変換するには、このFTPクライアントの開発に関する記事全体へのリンクです:

http://dot-net-talk.blogspot.com/2008/12/how-to-create-ftp-client-in-vbnet.html

0
protected void Button1_Click(object sender, EventArgs e) 
{ 
    string server = "-------"; //your ip address 
    string ftpPath = "ftp://" + server + "//Files//" +FileUpload1.FileName; 
    string uname = "-----"; 
    string password = "----------"; 
    string filePath = Server.MapPath("~") + "\\" + FileUpload1.FileName; 
    try 
    { 
     UploadToFTP(ftpPath,filePath,uname,password); 

    } 
    catch (Exception ex) 
    { 
     //logic here 

    }  

} 

`プライベートブールUploadToFTP(文字列strFTPFilePath、文字列strLocalFilePath、文字列strUserName、文字列strPassword) { を試してみてください{ // FTPリクエストオブジェクトとSpecfiyに完全なパスを作成します // System.Net .WebRequest.Create(strFTPFilePath); FtpWebRequest reqObj =(FtpWebRequest)WebRequest.Create(strFTPFilePath);

 //Call A FileUpload Method of FTP Request Object 
     reqObj.Method = WebRequestMethods.Ftp.UploadFile; 

     //If you want to access Resourse Protected,give UserName and PWD 
     reqObj.Credentials = new NetworkCredential(strUserName, strPassword); 

     // Copy the contents of the file to the byte array. 
     byte[] fileContents = File.ReadAllBytes(strLocalFilePath); 
     reqObj.ContentLength = fileContents.Length; 

     //Upload File to FTPServer 
     Stream requestStream = reqObj.GetRequestStream(); 
     // Stream requestStream = response.GetResponseStream(); 
     requestStream.Write(fileContents, 0, fileContents.Length); 
     requestStream.Close(); 
     FtpWebResponse response = (FtpWebResponse)reqObj.GetResponse(); 
     response.Close(); 
     Label1.Text = "File Transfered Completed" + response.StatusDescription; 
    } 

    catch (Exception Ex) 
    { 
     throw Ex; 
    } 
    return true; 
} ` 
関連する問題