This is a migrated thread and some comments may be shown as answers.

Read file data from AsyncUpload?

5 Answers 330 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 07 Dec 2010, 06:44 PM
Hi all:

I am working with this example:

http://demos.telerik.com/aspnet-ajax/upload/examples/async/webmail/defaultcs.aspx?product=asyncupload

However I do not understand how to read the file data from the uploaded files. I need to read the files into a byte array. Here is the code:

PopulateUploadedFilesList();
if (UploadedFiles != null)
{
  AttachmentRepeater.DataSource = UploadedFiles;
  AttachmentRepeater.DataBind();
  for (int i = 0; i<UploadedFiles.Count; i++)
  {
       #region get file content
       // Open a file and read the contents into a byte array.
       FileStream stream = File.OpenRead(UploadedFiles[i].FileName);
       byte[] byteData = new byte[stream.Length];
       stream.Read(byteData, 0, byteData.Length);
       stream.Close();
       // Encode the data using base64.
       string encodedData = System.Convert.ToBase64String(byteData);
     }
  }

Any and all help is greatly appreciated.

John.

5 Answers, 1 is accepted

Sort by
0
Genady Sergeev
Telerik team
answered on 09 Dec 2010, 12:58 PM
Hello John,

Here is how you can read an uploaded file into a byte array:

void RadAsyncUpload1_FileUploaded(object sender, Telerik.Web.UI.FileUploadedEventArgs e)
    {
        using (Stream str = e.File.InputStream)
        {
            byte[] content = new byte[str.Length];
            str.Read(content, 0, content.Length);
        }
    }

I hope that this helps.

Regards,
Genady Sergeev
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
John
Top achievements
Rank 1
answered on 09 Dec 2010, 05:20 PM
Thanks for your reply. Is there a way to hide the text box and just have the "Select" button visible?
0
Genady Sergeev
Telerik team
answered on 14 Dec 2010, 11:04 AM
Hello John,

Yes, this can be achieved with the following CSS:

.RadUpload input.ruFakeInput
        {
            display: none;
        }


Regards,
Genady Sergeev
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Wayne
Top achievements
Rank 1
answered on 22 Mar 2021, 09:23 PM
But how do you read the file? That is a text file, I want to read (loop each char) and append to a string.
0
Vessy
Telerik team
answered on 23 Mar 2021, 12:43 PM

Hi Wayne,

I have just answered your support ticket on the matter. For convenience, I am copying my reply here as well:

 

You can use StreamReader in order to read the file content directly and store it in the Session:

https://docs.microsoft.com/en-us/dotnet/api/system.io.streamreader.readtoend?view=net-5.0#System_IO_StreamReader_ReadToEnd

You can pass the InputStream of the uploaded file directly to the StreamReader and store the result in the Session:

    protected void RadAsyncUpload1_FileUploaded(object sender, Telerik.Web.UI.FileUploadedEventArgs e)
    {
        using (Stream stream = e.File.InputStream)
        {
            string fileOutput = new StreamReader(e.File.InputStream).ReadToEnd();
            Session[Session.SessionID + "UploadedFile"] = fileOutput;
        }
    }

 

The stored value can be accessed after that like follow:

    private string GetPRWinSettings()
    {
        string fileOutput = "";

        if (!object.Equals(Session[Session.SessionID + "UploadedFile"], null))
        {
            fileOutput = Session[Session.SessionID + "UploadedFile"].ToString();
            //your logic here
        }

        return fileOutput;
    }
 

Regards,
Vessy
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
AsyncUpload
Asked by
John
Top achievements
Rank 1
Answers by
Genady Sergeev
Telerik team
John
Top achievements
Rank 1
Wayne
Top achievements
Rank 1
Vessy
Telerik team
Share this question
or