简体   繁体   English

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

[英]Passing File name from textBox to WebRequest.Create in c#

Im working on some small app, and need to upload a text file to ftp. 我正在处理一些小型应用程序,需要将文本文件上传到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. 当上传的文件始终命名为myfile.txt时,问题就来了,我需要文件名与textBox1中的文本完全相同。

So for example i used this when saving file on harddrive ("C:/savehere/"+textBox1.text +".txt"); 因此,例如,我在将文件保存到硬盘驱动器时使用了此文件("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 ! 谢谢 !

If the path is ftp://www.mywebserver.com/filename.txt 如果路径为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");

        // ....

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

I understand that this is due to static, but i have no idea how to change it to correct type :( 我知道这是由于静态引起的,但是我不知道如何将其更改为正确的类型:(

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM