通过C#中的HTTP侦听器获取发布的文件 [英] Get posted file over HTTP Listener in c#

查看:48
本文介绍了通过C#中的HTTP侦听器获取发布的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用c#制作了一个简单的http服务器.而且我知道如何获取发布的数据并输出它们.这是我的C#代码

I have make a simple http server using c#. And I know how to get posted data and output them. Here is my c# code

public static void start(){
    HttpListener listener = new HttpListener();
    listener.Prefixes.Add(new Uri("http://localhost:8080").ToString());
    istener.Start();
    while(true){
        HttpListenerContext con = listener.GetContext();
        showPostedData(con.Request);
        con.Response.StatusCode = (int)HttpStatusCode.NotFound;
        string data = "Uploaded successful";
        byte[] output = Encoding.ASCII.GetBytes(data);
        con.Response.ContentType = "text/html";
        con.Response.ContentLength64 = output.Length;
        con.Response.OutputStream.Write(output , 0, output.Length );
    }
}
public static void showPostedData(HttpListenerRequest request){
    if (!request.HasEntityBody)
    {
        return;
    }
    System.IO.Stream body = request.InputStream;
    System.Text.Encoding encoding = request.ContentEncoding;
    System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
    string text;
    if (request.ContentType != null)
    {
        text = reader.ReadToEnd();
    }
    Console.WriteLine( text );
}

我的客户html表单具有名为文件的输入框:< input type ="file" name"file">

My client html form has input box named file: <input type="file" name"file">

在控制台中的输出是这样的:file ='所选文件的路径不是文件'

In the console output is like that : file='Path of Choosed file not a file'

所以我如何获取POSTED文件并将其复制到上载目录..?对不起,我的英语,谢谢...

So How i get POSTED files and copy them to upload directory..? Sorry for my english and thanks in advance...

推荐答案

public static void start(){
    HttpListener listener = new HttpListener();
    listener.Prefixes.Add(new Uri("http://localhost:80").ToString());
    istener.Start();

    while(true){
        HttpListenerContext con = listener.GetContext();

        var values = new HttpNameValueCollection(ref con);
        try
        {
            Console.WriteLine(values.Files["file"].FileName);
            File.WriteAllText(values.Files["file"].FileName, values.Files["file"].FileData, Encoding.Default);
        }
        catch (Exception tr)
        {

        }
    }
}
class HTTPFormData
{
    public class File
    {
        private string _fileName;
        public string FileName { get { return _fileName ?? (_fileName = ""); } set { _fileName = value; } }

        private string _fileData;
        public string FileData { get { return _fileData ?? (_fileName = ""); } set { _fileData = value; } }

        private string _contentType;
        public string ContentType { get { return _contentType ?? (_contentType = ""); } set { _contentType = value; } }
    }

    private NameValueCollection _post;
    private Dictionary<string, File> _files;
    private readonly HttpListenerContext _ctx;

    public NameValueCollection Post { get { return _post ?? (_post = new NameValueCollection()); } set { _post = value; } }
    public NameValueCollection Get { get { return _ctx.Request.QueryString; } }
    public Dictionary<string, File> Files { get { return _files ?? (_files = new Dictionary<string, File>()); } set { _files = value; } }

    private void PopulatePostMultiPart(string post_string)
    {
        var boundary_index = _ctx.Request.ContentType.IndexOf("boundary=") + 9;
        var boundary = _ctx.Request.ContentType.Substring(boundary_index, _ctx.Request.ContentType.Length - boundary_index);

        var upper_bound = post_string.Length - 4;

        if (post_string.Substring(2, boundary.Length) != boundary)
            throw (new InvalidDataException());

        var current_string = new StringBuilder();

        for (var x = 4 + boundary.Length; x < upper_bound; ++x)
        {
            if (post_string.Substring(x, boundary.Length) == boundary)
            {
                x += boundary.Length + 1;

                var post_variable_string = current_string.Remove(current_string.Length - 4, 4).ToString();

                var end_of_header = post_variable_string.IndexOf("\r\n\r\n");

                if (end_of_header == -1) throw (new InvalidDataException());

                var filename_index = post_variable_string.IndexOf("filename=\"", 0, end_of_header);
                var filename_starts = filename_index + 10;
                var content_type_starts = post_variable_string.IndexOf("Content-Type: ", 0, end_of_header) + 14;
                var name_starts = post_variable_string.IndexOf("name=\"") + 6;
                var data_starts = end_of_header + 4;

                if (filename_index != -1)
                {
                    var filename = post_variable_string.Substring(filename_starts, post_variable_string.IndexOf("\"", filename_starts) - filename_starts);
                    var content_type = post_variable_string.Substring(content_type_starts, post_variable_string.IndexOf("\r\n", content_type_starts) - content_type_starts);
                    var file_data = post_variable_string.Substring(data_starts, post_variable_string.Length - data_starts);
                    var name = post_variable_string.Substring(name_starts, post_variable_string.IndexOf("\"", name_starts) - name_starts);
                    Files.Add(name, new File() { FileName = filename, ContentType = content_type, FileData = file_data });
                }
                else
                {
                    var name = post_variable_string.Substring(name_starts, post_variable_string.IndexOf("\"", name_starts) - name_starts);
                    var value = post_variable_string.Substring(data_starts, post_variable_string.Length - data_starts);
                    Post.Add(name, value);
                }

                current_string.Clear();
                continue;
            }

            current_string.Append(post_string[x]);
        }
    }

    private void PopulatePost()
    {
        if (_ctx.Request.HttpMethod != "POST" || _ctx.Request.ContentType == null) return;

        var post_string = new StreamReader(_ctx.Request.InputStream, _ctx.Request.ContentEncoding).ReadToEnd();

        if (_ctx.Request.ContentType.StartsWith("multipart/form-data"))
            PopulatePostMultiPart(post_string);

    }

    public HTTPFormData(ref HttpListenerContext ctx)
    {
        _ctx = ctx;
        PopulatePost();
    }
}

这篇关于通过C#中的HTTP侦听器获取发布的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆