Saving the image uploaded in radupload to file system

Thread is closed for posting
4 posts, 0 answers
  1. vishesh kumar
    vishesh kumar avatar
    24 posts
    Member since:
    Sep 2009

    Posted 22 Nov 2010 Link to this post

    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;
            }
  2. Cori
    Cori avatar
    562 posts
    Member since:
    Jul 2010

    Posted 22 Nov 2010 Link to this post

    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.
  3. vishesh kumar
    vishesh kumar avatar
    24 posts
    Member since:
    Sep 2009

    Posted 22 Nov 2010 Link to this post

    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?
  4. Kalpna
    Kalpna avatar
    37 posts
    Member since:
    Jun 2013

    Posted 16 Jul 2013 Link to this post

    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.
Back to Top