在c#中将文件名从textBox传递到WebRequest.Create

Passing File name from textBox to WebRequest.Create in c#
2020-11-21
  •  译文(汉语)
  •  原文(英语)

我正在处理一些小型应用程序,需要将文本文件上传到ftp.

我使用此代码上传文件:

using System;
using System;
using System.IO;
using System.Net;
using System.Text;

/// <summary>
/// Simple static class for uploading a file to an FTP server.
/// </summary>
public static class fileUpload
{
    public static string uploadFile(string file)
    {
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.mywebserver.com/myfile.txt");
        request.Method = WebRequestMethods.Ftp.UploadFile;

        // This example assumes the FTP site uses anonymous logon.
        request.Credentials = new NetworkCredential("username", "password");

        // Copy the entire contents of the file to the request stream.
        StreamReader sourceStream = new StreamReader(file);
        byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
        sourceStream.Close();
        request.ContentLength = fileContents.Length;

        // Upload the file stream to the server.
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(fileContents, 0, fileContents.Length);
        requestStream.Close();

        // Get the response from the FTP server.
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        // Close the connection = Happy a FTP server.
        response.Close();

        // Return the status of the upload.
        return response.StatusDescription;

    }
}

要上传文件,我可以这样:

fileUpload.uploadFile(uploadedfile.txt);

当上传的文件始终命名为myfile.txt时,问题就来了,我需要文件名与textBox1中的文本完全相同.

因此,例如,我在将文件保存到硬盘驱动器时就使用了它,("C:/savehere/"+textBox1.text +".txt"); 并且效果很好.

但是当我在这里做同样的事情时:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.mywebserver.com/"+ textBox1.text + ".txt"); 

它不会工作.

在此示例中,我该怎么做?

谢谢 !

解决过程1

如果路径是 ftp://www.mywebserver.com/filename.txt

尝试更改为

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.mywebserver.com/%2f"+ textBox1.text + ".txt");

但是,最好创建一个目录来保留文件

ftp://www.mywebserver.com/%2ftemp/filename.txt

更新-

public static class fileUpload
{
    TextBox textBox1 = new TextBox();

    public static void getText(TextBox tb) {
         return tb.text;
    }

    public static string uploadFile(string file)
    {
        var aText = getText(textBox1);

        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.mywebserver.com/%2f"+ aText + ".txt");

        // ....
速聊1:
当我这样做时,它返回一个错误:Error 1非静态字段,方法或属性'WindowsFormsApplication1.Form1.textBox7'需要对象引用
速聊2:
我知道这是由于静态引起的,但我不知道如何将其更改为正确的类型:(
解决过程2

当我这样做时,它返回一个错误:Error 1非静态字段,方法或属性'WindowsFormsApplication1.Form1.textBox7需要对象引用

我知道这是由于静态引起的,但是我不知道如何将其更改为正确的类型:(

解决过程3

关于什么?

string baseUrl = "ftp://www.mywebserver.com/";
string FileName = textBox1.Text;
string extension = ".txt";

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(new Uri(baseUrl), string.Format("{0}{1}", FileName, extension)));

这将导致:

ftp://www.mywebserver.com/{textboxContent}.txt

Im working on some small app, and need to upload a text file to ftp.

I use this code to upload a file :

using System;
using System;
using System.IO;
using System.Net;
using System.Text;

/// <summary>
/// Simple static class for uploading a file to an FTP server.
/// </summary>
public static class fileUpload
{
    public static string uploadFile(string file)
    {
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.mywebserver.com/myfile.txt");
        request.Method = WebRequestMethods.Ftp.UploadFile;

        // This example assumes the FTP site uses anonymous logon.
        request.Credentials = new NetworkCredential("username", "password");

        // Copy the entire contents of the file to the request stream.
        StreamReader sourceStream = new StreamReader(file);
        byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
        sourceStream.Close();
        request.ContentLength = fileContents.Length;

        // Upload the file stream to the server.
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(fileContents, 0, fileContents.Length);
        requestStream.Close();

        // Get the response from the FTP server.
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        // Close the connection = Happy a FTP server.
        response.Close();

        // Return the status of the upload.
        return response.StatusDescription;

    }
}

To upload a file i use this :

fileUpload.uploadFile(uploadedfile.txt);

The problem comes when the file uploaded is always named us myfile.txt , i need the name of the file to be exactly as text in textBox1.

So for example i used this when saving file on harddrive ("C:/savehere/"+textBox1.text +".txt"); and it worked fine.

But when i do the same here :

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.mywebserver.com/"+ textBox1.text + ".txt"); 

it wont work.

How can i do the same in this example ?

Thank you !

Solutions1

If the path is ftp://www.mywebserver.com/filename.txt

Try change to

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.mywebserver.com/%2f"+ textBox1.text + ".txt");

However, it may be better to create a directory to keep the files

ftp://www.mywebserver.com/%2ftemp/filename.txt

UPDATE --

public static class fileUpload
{
    TextBox textBox1 = new TextBox();

    public static void getText(TextBox tb) {
         return tb.text;
    }

    public static string uploadFile(string file)
    {
        var aText = getText(textBox1);

        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.mywebserver.com/%2f"+ aText + ".txt");

        // ....
Talk1:
When i do that it comes back with an error :Error 1 An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.textBox7'
Talk2:
I understand that this is due to static, but i have no idea how to change it to correct type :(
Solutions2

When i do that it comes back with an error :Error 1 An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.textBox7

I understand that this is due to static, but i have no idea how to change it to correct type :(

Solutions3

What about?

string baseUrl = "ftp://www.mywebserver.com/";
string FileName = textBox1.Text;
string extension = ".txt";

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(new Uri(baseUrl), string.Format("{0}{1}", FileName, extension)));

this will result in:

ftp://www.mywebserver.com/{textboxContent}.txt
转载于:https://stackoverflow.com/questions/28362155/passing-file-name-from-textbox-to-webrequest-create-in-c-sharp

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入