2012-08-02 25 views
1

我想構建簡單的網絡服務器,可以傳輸文件。
我知道,有很多的例子,但大多過於複雜,瞭解的人誰從來沒有與HTTP工作
所以我有...使用HTTP響應傳輸文件的正確方法是什麼?

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 
} 

它適用於HTML轉讓,但我不能讓它傳輸圖像:(
,如果我做的HTML頁面本文標籤:

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

,並把這個文件到正確的目錄,但它仍然顯示在瀏覽器丟失的文件

+0

調試和檢查,如果work_folder + p.http_url是正確的,也許有一個\或路徑/失蹤? – user1519979 2012-08-02 14:58:09

+0

沒有缺失。文件是成功的 – Kosmos 2012-08-02 14:58:54

回答

3

我認爲你犯了很多錯誤。

  • 您假定您可以避免示例代碼的所有複雜性。
  • 而不是粘貼你的代碼,並讓別人做你的工作,你應該教你自己的HTTP - 它不應該太難你的任務範圍
  • 你正在編寫的代碼做一些可以通過IIS它運行的代碼(如果運行在IIS上的代碼)
  • 您與p.outputStream.Write(Encoding.ASCII.GetString(file_content)); //write file content after headers

寫入文件作爲字符串,我建議:

+1

1.我成功避免了所有的複雜性,並修改了源代碼,它只能將由代碼頁生成的靜態代碼發送到能夠從HDD文件發送讀取的代碼。2.我從來沒有要求某人做我的工作,我剛問過有什麼問題? 3.我需要在我的主應用程序中嵌入http服務器,它與IIS沒有任何連接,我不想讓用戶安裝IIS來運行我的應用程序。 4.謝謝,這就是我想知道的一切 – Kosmos 2012-08-02 16:09:51

+0

我建議Tymek在回答問題時不要粗魯。 – 2015-07-04 14:17:12

相關問題