Connecting Tech Pros Worldwide Forums | Help | Site Map

Reading content of a file uploaded by user

Newbie
 
Join Date: Jun 2009
Posts: 1
#1: Jun 30 '09
Hi everyone, new user here. I've been finding it harder and harder to get answers at EE for certain questions, and am hoping someone here will be able to help me!

Using asp.net and c#, my site allows users to save the innerhtml of a certain div to a file. Now I need the reverse.

Here's what I need: upon clicking a button, the user gets a "open file" dialog, which let him choose a file on his pc. Upon choosing it, the site reads the content of the file, and assigns it as the innerhtml of a div.

several points: 1) can this be done without saving the file to the server? I have no need for the actual file, just the contents of it.

2) I would ideally want the file to have a proprietary file extension, so the user doesnt by mistake upload a wrong file. I've been saving the file using

Expand|Select|Wrap|Line Numbers
  1.     Response.ClearHeaders();
  2.     Response.ClearContent();
  3.     Response.AddHeader("content-type", "text/plain");
  4.     Response.AddHeader("Content-disposition", "attachment; filename=yourname.txt");
  5.     Response.Write(divTest.InnerHtml);
  6.     Response.Flush();
  7.     Response.End();
Can I use this for a proprietary file.extension?

3) Should I be worried about hacking? I.e. can someone put malicious code in the file which, upon loaded as the innerhtml of the div, will cause me havoc?

TIA!

insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#2: Jul 1 '09

re: Reading content of a file uploaded by user


1) No. The only way you will be able to do anything with the file is to save it somewhere on your server. Likely the best course of action will be to save it to a /temp folder and delete it when you are done with it.

2) Yes, this will work with any file extension you choose. It may cause your users problems if you give it an extension that another program already uses on their system, but you shouldn't have a problem, since you are effectively going to read the file's bytes and encode them to text.

3) Possibly. Allowing users to insert raw HTML/CSS/Javascript into your page is probably a bad idea. It could be used for XSS Attacks. I would suggest that you parse the file that they are passing in and strip out anything that doesn't conform to what you expect.

That being said, Welcome to Bytes! I hope you find the help you need.
Newbie
 
Join Date: Oct 2008
Location: Islamabad, Pakistan
Posts: 9
#3: Jul 8 '09

re: Reading content of a file uploaded by user


You can do this for only reading the contents of the file updloaded by user but you have to provide Asynchronous call back.

FileUpload1.FileContent.BeginRead( ....................);
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,118
#4: Jul 8 '09

re: Reading content of a file uploaded by user


I'm not sure what sharifee's talking about because (unless some upgrade's happend that I'm unaware of) the ASP.NET FileUpload control does not work in an UpdatePanel which allows you to preform an asynchronous postback to the server (to upload/save the file)

When developing web applications you always have a clear understanding about what's executed client side (in the browser) and what's executed server side.

Your .NET code works server side. It cannot do anything client side...for example you cannot use .NET code to open a File Open Dialogue because the File Open Dialogue is run in the client's browser.

The only way you can actually get this to appear is using the ASP.NET FileUpload control...which is rendered as an <input type="file">.

There are a lot of restrictions with respect to the FileUpload control because of the potential security risks of allowing a website open files....imagine if a FileUpload object were able to read files client side.....it could be used to read any file (like a password, or banking information file).

So, the FileUpload control can only be used to upload files to the server. The path that it contains cannot be modified using JavaScript and you cannot set the path using .NET code either.

With this in mind you can see that you must upload the file to the server before you can read it's contents.

After that point you can use your C# code to set the innerHtml of the element that is being used to display the file contents.

Better yet, you should consider using an ASP.NET Localize control. The Localize control can be used to print text into the web page without any HTML tags around it.

For example, if you had something like:
Expand|Select|Wrap|Line Numbers
  1. <div>
  2. <asp:Label ID="SectionWhereTheFileContentIsDisplayed" runat="server"></asp:Label>
  3. </div>
  4.  
The HTML generated for the above would look like:
Expand|Select|Wrap|Line Numbers
  1. <div>
  2.   <span ID="SectionWhereTheFileContentIsDisplayed"> file content </span>
  3. </div>
But if you were to use a Localize control instead:
Expand|Select|Wrap|Line Numbers
  1. <div>
  2. <asp:Locolize ID="SectionWhereTheFileContentIsDisplayed" runat="server"></asp:Locolize >
  3. </div>
  4.  
The page would be rendered as:
Expand|Select|Wrap|Line Numbers
  1. <div>
  2.    file content 
  3. </div>
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,118
#5: Jul 8 '09

re: Reading content of a file uploaded by user


I just looked it up.
The UpdatePanel does not support the FileUpload control. If you put a FileUpload control in an UpdatePanel the page will postback like normal but the file will not be uploaded to the server...it will be "null"/"nothing" in your C# code.


Quote:

Originally Posted by msdn

Controls that Are Not Compatible with UpdatePanel Controls

The following ASP.NET controls are not compatible with partial-page updates, and are therefore not designed to work inside an UpdatePanel control:

TreeView control under several conditions. One is when callbacks are enabled that are not part of an asynchronous postback. Another is when you set styles directly as control properties instead of implicitly styling the control by using a reference to CSS styles. Another is when the EnableClientScript property is false (the default is true). Another is if you change the value of the EnableClientScript property between asynchronous postbacks. For more information, see TreeView Web Server Control Overview.

Menu control when you set styles directly as control properties instead of implicitly styling the control by using a reference to CSS styles. For more information, see Menu Control Overview.

FileUpload and HtmlInputFile controls when they are used to upload files as part of an asynchronous postback.

GridView and DetailsView controls when their EnableSortingAndPagingCallbacks property is set to true. The default is false.

Login, PasswordRecovery, ChangePassword, and CreateUserWizard controls whose contents have not been converted to editable templates.

The Substitution control.

To use a FileUpload or HtmlInputFile control inside an UpdatePanel control, set the postback control that submits the file to be a PostBackTrigger control for the panel. The FileUpload and HtmlInputFile control can be used only in postback scenarios.

All other controls work inside UpdatePanel controls. However, in some circumstances, a control might not work as expected inside an UpdatePanel control. These circumstances include the following:


Registering script by calling registration methods of the ClientScriptManager control.

Rendering script or markup directly during control rendering, such as by calling the Write method.

If the control calls script registration methods of the ClientScriptManager control, you might be able to use corresponding script registration methods of the ScriptManager control instead. In that case, the control can work inside an UpdatePanel control.

Newbie
 
Join Date: Oct 2008
Location: Islamabad, Pakistan
Posts: 9
#6: Jul 9 '09

re: Reading content of a file uploaded by user


i have tested a small application in which i have read a txt file and dislplayed the text on label control without saving it on server. You can read file in bytes array.


Expand|Select|Wrap|Line Numbers
  1. using System.IO;
  2.  
  3. public partial class _Default : System.Web.UI.Page 
  4. {
  5.     byte[] byt;
  6.     protected void Page_Load(object sender, EventArgs e)
  7.     {
  8.  
  9.     }
  10.     protected void Button1_Click(object sender, EventArgs e)
  11.     {
  12.  
  13.         if (FileUpload1.HasFile)
  14.         {
  15.           byt = FileUpload1.FileBytes;
  16.         }
  17.         string str = System.Text.Encoding.UTF8.GetString(byt);
  18.  
  19.         Label lbl = new Label();
  20.         lbl.Text = str;
  21.         Panel1.Controls.Add(lbl);
  22.     }
  23. }

above code i tested for txt file. selected in FileUpload Control .
sorry for wording my English is not good.
Reply

Tags
asp.net, csharp