?
Solved

Reading and writing the file in asp.net

Posted on 2009-12-26
3
Medium Priority
?
227 Views
Last Modified: 2013-11-26
hello,

my scanrio is that:

for reading via fileupload:
i have fileupload button in my webpage when user hit the button the file upload from the server and then i read the content of the given file and place the content in the textbox and also write the server path in the session varible full server path,

for writing via savdialog:
i show the download button to the user when user hit the button i have a text in my textbox, when user click the content of the textbox is save via 3 format txt, psd, doc constraint the user when save dialog box is show.

i show the gui below and remember all the contents are dynamic not static so must care for code

Note:
1- Don't give me reference URL (strickly prohabited)
2- send me the working code which which meets up my case
3- Working Code must..

case2258.jpg
0
Comment
Question by:AVARICESOFT
  • 3
3 Comments
 
LVL 5

Expert Comment

by:muhammadyasir
ID: 26126736
use streamreader and streamwriter object in c#
0
 
LVL 5

Expert Comment

by:muhammadyasir
ID: 26126746

///For reading:
   string list ="";
   StreamReader reader = new StreamReader("file.txt");
   line = reader.ReadToEnd();
   reader.Close();

///For Writing:
    StreamWriter wrt = new StreamWriter("file.txt");
    wrt.Write("your text to right"):
    wrt.Close();

///For Downloading;
      // 1.
        // Get path of byte file.
        string path = Server.MapPath("~/Adobe2.png");

        // 2.
        // Get byte array of file.
        byte[] byteArray = File.ReadAllBytes(path);

        // 3A.
        // Write byte array with BinaryWrite.
        Response.BinaryWrite(byteArray);

        // 3B.
        // Write with OutputStream.Write [commented out]
        // Response.OutputStream.Write(byteArray, 0, byteArray.Length);

        // 4.
        // Set content type.
        Response.ContentType = "image/png";

Open in new window

0
 
LVL 5

Accepted Solution

by:
muhammadyasir earned 2000 total points
ID: 26126750
some mistakes in above code.
use this code
///For reading:
   string list ="";
   StreamReader reader = new StreamReader("file.txt");
   list  = reader.ReadToEnd();
   reader.Close();

///For Writing:
    StreamWriter wrt = new StreamWriter("file.txt");
    wrt.Write("your text to write"):
    wrt.Close();

///For Downloading;
      // 1.
        // Get path of byte file.
        string path = Server.MapPath("~/Adobe2.png");

        // 2.
        // Get byte array of file.
        byte[] byteArray = File.ReadAllBytes(path);

        // 3A.
        // Write byte array with BinaryWrite.
        Response.BinaryWrite(byteArray);

        // 3B.
        // Write with OutputStream.Write [commented out]
        // Response.OutputStream.Write(byteArray, 0, byteArray.Length);

        // 4.
        // Set content type.
        Response.ContentType = "image/png";

Open in new window

0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question