Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8331127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:19:29+00:00 2026-06-09T02:19:29+00:00

i am trying to build simple web server, that able to transfer files. I

  • 0

i am trying to build simple web server, that able to transfer files.
I know, there is a lot of examples, but most of them too complex to understand for someone who never worked with HTTP
So i have…

    public Hashtable MimeTypes = new Hashtable();

    public HttpServer(int port)
    {
        this.port = port;

        MimeTypes.Add("html", "text/html");
        MimeTypes.Add("htm", "text/html");
        MimeTypes.Add("css", "text/css");
        MimeTypes.Add("js", "application/x-javascript");

        MimeTypes.Add("png", "image/png");
        MimeTypes.Add("gif", "image/gif");
        MimeTypes.Add("jpg", "image/jpeg");
        MimeTypes.Add("jpeg", "image/jpeg");
    }

    public void writeSuccess(string mime_type, string file_name, int file_size)
    {
        outputStream.Write("HTTP/1.0 200 OK\n");
        outputStream.Write("Content-Type: " + mime_type + "\n");

        if (file_name != null)//if file name isn't null, this mean we need to add additional headers
        {
            outputStream.Write("Content-Disposition: attachment; filename=" + file_name);
            outputStream.Write("Content-Length: " + file_size);
        }

        outputStream.Write("Connection: close\n");
        outputStream.Write("\n");
    }

public override void handleGETRequest(HttpProcessor p)
{
    Console.WriteLine("request: {0}", p.http_url);

    byte[] file_content = null;

    try { file_content = File.ReadAllBytes(work_folder + p.http_url); } //tring to read requested file
    catch (Exception exc) { p.writeFailure(); return; } //return failure if no such file

    string[] splitted_html_url = p.http_url.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries ); //splitting url for future format check

    string mime_type = "application/octet-stream"; //the most generic type

    if (MimeTypes.Contains(splitted_html_url[splitted_html_url.Length - 1]))
        mime_type = (string)MimeTypes[splitted_html_url[splitted_html_url.Length - 1]]; //set mimy type that math to requested file format

    if (mime_type.Contains("image") || mime_type == "application/octet-stream") //hacky thing for tests...
        p.writeSuccess(mime_type, p.http_url.Remove(0, 1), file_content.Length); //if mime type is image or unknown, than pass file name and length to responce builder
    else
        p.writeSuccess(mime_type, null, 0); //er else just add general headers

    p.outputStream.Write(Encoding.ASCII.GetString(file_content)); //write file content after headers
}

It works for HTML transfers, but i can’t make it transfer images 🙁
If i make html page with this tag:

<img src = "logo225x90.gif" width = "100%" height = "100%" />

and place this file to right directory, it still showed in browser as missing file

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:19:30+00:00Added an answer on June 9, 2026 at 2:19 am

    I think you are making multiple mistakes.

    • You are assuming that you can avoid all the complexity of the example code.
    • Instead of pasting your code and make somebody do your work you should educate yourself about HTTP – it shouldn’t be too hard for the scope of your task
    • You are writing code to do something which can be done by IIS which runs your code (if you run your code on IIS)
    • You are writing the file as string with p.outputStream.Write(Encoding.ASCII.GetString(file_content)); //write file content after headers

    I suggest:

    • Don’t use the CodeProject example you are using;
    • Try the ServiceStack project (your may want to read ServiceStack and returning a stream)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When trying to build a simple test program that uses atomic operations, I get
I am trying to build a simple script which must be able to navigate
I am trying to build a simple server to communicate with Arduino and store
I'm trying to build simple facebook application with 4.2.1 C# SDK. But I have
I'm trying to build a simple web application using Node.js and CouchDb. The application
I am new to RoR and I am trying to build simple web application
I am a newbie coder trying to build a simple web app using PHP.
I'm writing a very simple web crawler and trying to parse 'robots.txt' files. I
I just started trying to build simple GUIs because I've always used command-line script
i am trying to build simple php crawler for this purpose i am getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.