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

时间:2015-02-06 09:10:29

标签: c# file

我在处理一些小应用程序,需要将文本文件上传到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"); 

它不会工作。

在这个例子中我该怎么做?

谢谢!

3 个答案:

答案 0 :(得分:0)

如果路径为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 :(得分:0)

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

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

答案 2 :(得分:0)

怎么样?

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