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

Saving the image uploaded in radupload to file system

3 Answers 206 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
vishesh kumar
Top achievements
Rank 1
vishesh kumar asked on 22 Nov 2010, 10:48 AM
Hi,

I am trying to upload an image using the radupload control and save it on the file system. 

I need to convert the file stream to a byte array and then save it. 

While debugging, i found that 
coll[0].InputStream.Read(byteFile, 0, byteFile.Length + 1); 

the byteFile has 0 in all the position (which means its a corrupted file).


Can you please provide the code using which i can convert this input stream into byte array and save it on the file system?

foreach (RepeaterItem item in this.uploadRepeater.Items)
                            {
                                int imageId;
                                UploadedFileCollection coll = ((RadUpload)item.FindControl("radUpload")).UploadedFiles;
                                RadUpload up = ((RadUpload)item.FindControl("radUpload"));
                                if (null != coll && 0 != coll.Count)
                                {
                                    fileManager = new FileManager(coll);
                                    isImageValid = isImageValid & fileManager.ValidateFile();
 
                                    if (true == isImageValid)
                                    {
                                        byte[] byteFile = new byte[coll[0].InputStream.Length];
                                        coll[0].InputStream.Read(byteFile, 0, byteFile.Length + 1); // reading into //a byte arrary. Here while debugging, i see that all location in the array have value 0.                                       
                                    Guid guid =  this.SaveFileToDisk(byteFile , coll[0].GetExtension());
                                      
                                         
                                    }
                                    else
                                    {
                    // show error message
                                    }
                                }
                                 
                            }
 
 
 
 
 
 
        public static Guid SaveFileToDisk(byte[] fileBytes, string fileExtension)
        {
 
            Guid imageGuid = Guid.Empty;
            FileStream stream = null;
 
 
 
            imageGuid = Guid.NewGuid();
            string fileLocation = ConfigurationManager.Configurations().ImageSaveLocation + imageGuid + "." + fileExtension;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                // Open file for reading .
                using (stream = new FileStream(fileLocation, FileMode.Create, FileAccess.Write))
                {
                    // Writes a block of bytes to this stream using data from a byte array.
                    stream.Write(fileBytes, 0, fileBytes.Length);
                     
                }
            });
            return imageGuid;
        }

3 Answers, 1 is accepted

Sort by
0
Cori
Top achievements
Rank 2
answered on 22 Nov 2010, 02:53 PM
Hello Vishesh,

Is there a reason why you read the file by adding 1 to the length of the byte array? Perhaps that is the issue.
0
vishesh kumar
Top achievements
Rank 1
answered on 22 Nov 2010, 05:09 PM
I have tried that also. It is yielding the same result.

Update: I removed the upload control from the repeater and everything works file now. 
Don't know how and why this happens or I am missing something?
0
Kalpna
Top achievements
Rank 1
answered on 16 Jul 2013, 01:11 PM
I want to add a file with the guid but it is not saved in the folder. The file is saved as its original name. but i want its guid. and after it i also want to fetch or download this file from the folder. Please help me.
Tags
Upload (Obsolete)
Asked by
vishesh kumar
Top achievements
Rank 1
Answers by
Cori
Top achievements
Rank 2
vishesh kumar
Top achievements
Rank 1
Kalpna
Top achievements
Rank 1
Share this question
or