473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reading content of a file uploaded by user

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!
Jun 30 '09 #1
6 10427
Curtis Rutland
3,256 Expert 2GB
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.
Jul 1 '09 #2
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( ....................);
Jul 8 '09 #3
Frinavale
9,735 Expert Mod 8TB
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>
Jul 8 '09 #4
Frinavale
9,735 Expert Mod 8TB
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.


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.
Jul 8 '09 #5
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.
Jul 9 '09 #6
Thanks sharifee. This is exactly what I was looking for.
Apr 11 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Rea Peleg | last post by:
link to configuration file prevents user control download to internet explorer Hi I have a simple web application which consists an opening html page with a (windows form user) control which...
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
3
by: Bijoy Naick | last post by:
I've written a simple file upload user control in VB .NET. It comprises of an InputFile HTML Server Control, an Upload button and a message label. User clicks on the Browse button of the...
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
5
by: v.anupama | last post by:
I have a requirement where a user can access ONLY one file at any given time out of a list of files listed in datagrid (button/hyperlink columns). Only files listed are pdf,doc,txt. I cannot use...
11
by: trs-ggcsea | last post by:
Hi, I am reading a file from a UNC path in my Visual studio 2005 C# (.NET 2) program and getting an access denied exception. I am unsure if this access denied is due to the .NET security...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
4
by: archana | last post by:
Hi all, I want to read csv file into datatable. Is there any csv reader and writer available for freeware. I am reading csv file using schema.ini file. I don't want this dependency. The...
4
by: google.com | last post by:
Hi there! I've been digging around looking for a sample on how to upload a file without user action. I found the following article covering the area: ...
5
by: sagiv | last post by:
Can someone please send me an example of reading zip file (of text ffile) and write it to another text file. For example: If "a.txt" file was zipped to "a.zip" how can i read it and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.