locked
fileUpload control in asp.net RRS feed

  • Question

  • User1668758956 posted

     

     hi 

               im using fileupload in my application and i have i installed in the server.when im using to access the application from client machine and try to uplaod a file im getting the error the file not found.here is the code for file uploading which i wrote in my application

     

    FileStream fStream = File.OpenRead(filepath);

    byte[] contents = new byte[fStream.Length];

    fStream.Read(contents, 0, (int)fStream.Length);

    fStream.Close();

    MemoryStream ms = new MemoryStream(contents);

     

     

    how can i solve his problem?????????

    Tuesday, November 28, 2006 12:44 AM

All replies

  • User300685930 posted

    Not sure what you are doing, but here is a working example.  Not all the api's I use are here, but you shoud be able to get the gist of it.

     

      // try grabbing the image specified
                FileUpload fileUpload = (FileUpload)CaptchaUltimateControl1.FindControl("FileUpload1");
                Byte[] smallerImageBytes = null;
                Byte[] imageBytes = null;

                try
                {
                    if (fileUpload != null && fileUpload.HasFile)
                    {
                        imageBytes = fileUpload.FileBytes;
                        if (imageBytes.Length > 1000000)
                        {
                            completionStatus = false;
                            completionMessage = "Image May not be over 1Meg, try again";
                        }
                        else
                        {
                            MemoryStream ms = new MemoryStream(imageBytes);
                            string fName = string.Empty;
                            if (!string.IsNullOrEmpty(fileUpload.FileName))
                            {
                                fName = fileUpload.FileName;
                            }
                            smallerImageBytes = CodeCampSV.Utils.ResizeFromStream(fName, CodeCampSV.Utils.ThumbSize, ms);
                            completionStatus = true;
                        }
                    }
                }
                catch (Exception eee)
                {
                    throw new ApplicationException(eee.ToString());
                }

     

    and here is the resize from stream stuff:

     public static byte[] ResizeFromByteArray(string fileName, int MaxSideSize, Byte[] byteArrayIn)
            {
                byte[] byteArray = null;  // really make this an error gif
                MemoryStream ms = new MemoryStream(byteArrayIn);
                byteArray = CodeCampSV.Utils.ResizeFromStream(fileName, MaxSideSize, ms);



                return byteArray;
            }

     

     

     public static byte[] ResizeFromStream(string fileName, int MaxSideSize, Stream Buffer)
            {
                byte[] byteArray = null;  // really make this an error gif

                try
                {

                   


                    Bitmap bitMap = new Bitmap(Buffer);
                    int intOldWidth = bitMap.Width;
                    int intOldHeight = bitMap.Height;

                    int intNewWidth;
                    int intNewHeight;

                    int intMaxSide;

                    if (intOldWidth >= intOldHeight)
                    {
                        intMaxSide = intOldWidth;
                    }
                    else
                    {
                        intMaxSide = intOldHeight;
                    }

                    if (intMaxSide > MaxSideSize)
                    {
                        //set new width and height
                        double dblCoef = MaxSideSize / (double)intMaxSide;
                        intNewWidth = Convert.ToInt32(dblCoef * intOldWidth);
                        intNewHeight = Convert.ToInt32(dblCoef * intOldHeight);
                    }
                    else
                    {
                        intNewWidth = intOldWidth;
                        intNewHeight = intOldHeight;
                    }

                    Size ThumbNailSize = new Size(intNewWidth, intNewHeight);
                    System.Drawing.Image oImg = System.Drawing.Image.FromStream(Buffer);
                    System.Drawing.Image oThumbNail = new Bitmap
                        (ThumbNailSize.Width, ThumbNailSize.Height);
                    Graphics oGraphic = Graphics.FromImage(oThumbNail);
                    oGraphic.CompositingQuality = CompositingQuality.HighQuality;
                    oGraphic.SmoothingMode = SmoothingMode.HighQuality;
                    oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    Rectangle oRectangle = new Rectangle
                        (0, 0, ThumbNailSize.Width, ThumbNailSize.Height);

                    oGraphic.DrawImage(oImg, oRectangle);

                    //string fileName = Context.Server.MapPath("~/App_Data/") + "test1.jpg";
                    //oThumbNail.Save(fileName, ImageFormat.Jpeg);
                    MemoryStream ms = new MemoryStream();
                    oThumbNail.Save(ms, ImageFormat.Jpeg);
                    byteArray = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(byteArray, 0, Convert.ToInt32(ms.Length));

                    oGraphic.Dispose();
                    oImg.Dispose();
                    ms.Close();
                    ms.Dispose();
                }
                catch (Exception)
                {
                    int newSize = MaxSideSize - 20;
                    Bitmap bitMap = new Bitmap(newSize, newSize);
                    Graphics g = Graphics.FromImage(bitMap);
                    g.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(0, 0, newSize, newSize));

                    Font font = new Font("Courier", 8);
                    SolidBrush solidBrush = new SolidBrush(Color.Red);
                    g.DrawString("Failed File", font, solidBrush, 10, 5);
                    g.DrawString(fileName, font, solidBrush, 10, 50);

                    MemoryStream ms = new MemoryStream();
                    bitMap.Save(ms, ImageFormat.Jpeg);
                    byteArray = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(byteArray, 0, Convert.ToInt32(ms.Length));

                    ms.Close();
                    ms.Dispose();
                    bitMap.Dispose();
                    solidBrush.Dispose();
                    g.Dispose();
                    font.Dispose();

                }
                return byteArray;
            }

     

     

     

    Tuesday, November 28, 2006 2:16 AM