帮助上传到FTP

编程入门 行业动态 更新时间:2024-10-23 02:10:01
本文介绍了帮助上传到FTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我收到此错误:

I get this error:

"System.dll中发生了类型为'System.Net.WebException'的未处理的异常

"An unhandled exception of type 'System.Net.WebException' occurred in System.dll

附加信息:所请求的URI对于此FTP命令无效."

Additional information: The requested URI is invalid for this FTP command."

我仍无法发布屏幕截图,但是它发生在以下行:"Stream requestStream = request.GetRequestStream();"

I cant post a screenshot yet but it occurs for this line: "Stream requestStream = request.GetRequestStream();"

该程序读取和/或写入文本文件,然后上传到ftp.

The program reads and/or writes a text file and uploads to ftp.

我正在从msdn站点使用此代码:

I am using this code from the msdn site:

using System; using System.IO; using System.Net; using System.Text; namespace Examples.System.Net { public class WebRequestGetExample { public static void Main () { // Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso/test.htm"); request.Method = WebRequestMethods.Ftp.UploadFile; // This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso"); // Copy the contents of the file to the request stream. StreamReader sourceStream = new StreamReader("testfile.txt"); byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); sourceStream.Close(); request.ContentLength = fileContents.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(fileContents, 0, fileContents.Length); requestStream.Close(); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription); response.Close(); } } } }

这是完整的代码,少了登录凭据.

Here is the full code less login credentials.

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Data.SqlClient; using System.Net; namespace Streamer { public partial class Form1 : Form { private string User = "user"; private string Password = "password"; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { OpenFileDialog of = new OpenFileDialog(); of.ShowDialog(); textBox1.Text = of.FileName; } private void button2_Click(object sender, EventArgs e) { StreamReader sr = new StreamReader(textBox1.Text); textBox2.Text = sr.ReadToEnd(); sr.Close(); } private void button3_Click(object sender, EventArgs e) { StreamWriter sw = new StreamWriter(textBox1.Text, false);//writeToFileLocation sw.WriteLine(textBox3.Text);//writeFrom sw.Close(); } private void textBox1_TextChanged(object sender, EventArgs e) { } private void textBox2_TextChanged(object sender, EventArgs e) { } private void textBox3_TextChanged(object sender, EventArgs e) { } private void button4_Click(object sender, EventArgs e) { // StreamWriter sw = new StreamWriter(textBox1.Text, false);//writeToFileLocation // sw.WriteLine(textBox3.Text);//writeFrom //sw.Close(); // Get the object used to communicate with the server. // Get the object used to communicate with the server. // Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://files.000webhost/public_html/members/zones/" + textBox3.Text.Trim()); request.Method = WebRequestMethods.Ftp.UploadFile; // This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential(this.User, this.Password); // Copy the contents of the file to the request stream. StreamReader sourceStream = new StreamReader("streamer.txt"); byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); sourceStream.Close(); request.ContentLength = fileContents.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(fileContents, 0, fileContents.Length); requestStream.Close(); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription); response.Close(); // } } }

推荐答案

AlgoTraderX,

Hi AlgoTraderX,

>>>>" FtpWebRequest request =(FtpWebRequest)WebRequest.Create(< ftp://files.000webhost/public_html/members/zones/ " + textBox3.Text.Trim());

>>"FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://files.000webhost/public_html/members/zones/" + textBox3.Text.Trim());

感谢您的信息. textBox3的值是什么?上传FTP文件需要一个以文件名结尾的URL.因此您的网址应该是这样.

Thank you for your post. What is the value of textBox3? FTP file upload need a URL which endwith a file name. So your URL should be like this.

ftp://files.000webhost/public_html/members/zones/streamer.txt

最好的问候, 王丽

Best Regards, Li Wang

更多推荐

帮助上传到FTP

本文发布于:2023-11-10 00:55:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573920.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:FTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!