When I upload a text file from client to server using stream, how can I guarantee that the uploaded text file can be displayed correctly?


When I upload a text file from client to server using stream, how can I guarantee that the uploaded text file can be displayed correctly?
A user want to upload a text file from client PC to server (normally it is Windows 2003), but I'm not sure which OS it is in client, maybe it is a english version Windows or chinese Windows (default encoding is GB2312).
so the encoding of text file in client maybe is ASCII or GB2312.
I think that "StreamReader myRead= new StreamReader(FileUpload1.PostedFile.InputStream,Encoding.Default) will be run in server,
so "Encoding.Default" will be the default encoding of Windows 2003 server, if the default encoding of the Windows 2003 server is english encoding,
a uploaded text file with GB2312 encoding will be displayed correctly (I think), how can I guarantee that the uploaded text file can be displayed?
I think if the server can know the encoding of client which upload file, the program in server can replace "Encoding.Default" with "Client Encoding",
so no matter which OS the client is, the uploaded text file always can be displayed correctly in server!
Any ideas for me, many thanks!
 
 
//------------------------------------Upload program--------------------------------------------------------------------
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>">
<script runat="server">
  protected void UploadButton_Click(object sender, EventArgs e)
  {
    String savePath = Server.MapPath("~/upload/");
 
    if (FileUpload1.HasFile)
    {
      String fileName = FileUpload1.FileName;
      savePath += fileName;
      System.IO.StreamReader myRead= new System.IO.StreamReader(FileUpload1.PostedFile.InputStream,Encoding.Default);
      System.IO.StreamWriter myWrite = new System.IO.StreamWriter(savePath, false, Encoding.Unicode);
      string line = myRead.ReadLine();
       
      while (line != null)
      {
          myWrite.WriteLine(line);
          line=myRead.ReadLine();         
      }
       
      myWrite.Close();
      myRead.Close();
   
      UploadStatusLabel.Text = "Your file was saved as " + fileName;
    }
    else
    {     
      UploadStatusLabel.Text = "You did not specify a file to upload.";
    }
  }
</script>
<html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>" >
<head id="Head1" runat="server">
    <title>FileUpload Example</title> <meta name="viewport" content="width=device-width">
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <h4>Select a text file to upload:</h4>
  
       <asp:FileUpload id="FileUpload1"                
           runat="server">
       </asp:FileUpload>
           
      
      
       <asp:Button id="UploadButton"
           Text="Upload file"
           OnClick="UploadButton_Click"
           runat="server">
       </asp:Button>   
      
       <hr />
      
       <asp:Label id="UploadStatusLabel"
           runat="server">
       </asp:Label>       
    </div>
    </form>
</body>
</html>
//---------------------------------------Upload program------------------------------------------------------------------

SuperCool Multiple ZIP - A utility to unzip multiple files and work with multiple zip files

SuperCool Random Number Generator
0
mycwcgr
10/31/2005 1:33:36 AM
📁 asp.net.web-dev-2005-express
📃 3977 articles.
⭐ 0 followers.

💬 0 Replies
👁️‍🗨️ 1730 Views


Reply: