0

I need help in converting a file received from a jquery ajax to byte array. I'm using a plugin called ajaxfileupload then from a jquery ajax call I send a file from a fileupload control to a handler. Here is my handler code:

if (context.Request.Files.Count > 0)
{
    string path = context.Server.MapPath("~/Temp");
    if (!Directory.Exists(path))
        Directory.CreateDirectory(path);

    var file = context.Request.Files[0];

    string fileName;

    if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")
    {
        string[] files = file.FileName.Split(new char[] { '\\' });
        fileName = files[files.Length - 1];
    }
    else
    {
        fileName = file.FileName;
    }
    string fileType = file.ContentType;
    string strFileName = fileName;

    FileStream fs = new FileStream("~/Temp/" + strFileName, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fs);
    Byte[] imagebytes = br.ReadBytes((Int32)fs.Length);
    br.Close();
    fs.Close();

    DBAccess dbacc = new DBAccess();
    dbacc.saveImage(imagebytes);

    string msg = "{";
    msg += string.Format("error:'{0}',\n", string.Empty);
    msg += string.Format("msg:'{0}'\n", strFileName);
    msg += "}";
    context.Response.Write(msg);
}

I'm saving the file to a folder within a project then trying to retrieve that file and save it to the database. I can assure you that the image is being saved to the temp folder. The problem is with the line with (*) the file path is wrong. This is the file path that is being retrieved. "'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\~\Temp\2012-06-03 01.25.47.jpg'.". The temp folder is located locally inside my project and I want to retrieved the image within that folder. How can I set the file path to my desired location? Or is there another way to convert a file to byte array after retrieving it from a jquery ajax call?

Credits to these articles:

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Luke Villanueva
  • 2,030
  • 8
  • 44
  • 94
  • Consider this article: [An unexcited look at browser sniffing](http://balpha.de/2012/07/an-unexcited-look-at-browser-sniffing/) – abatishchev Jul 10 '12 at 15:13

2 Answers2

1

Just these 3 lines will do:

    int filelength = file.ContentLength;
    byte[] imagebytes = new byte[filelength ];
    file.InputStream.Read(imagebytes , 0, filelength );
SSA
  • 5,433
  • 4
  • 36
  • 50
  • Wow! I can't believe it could be that easy. All the articles I have read are all particular to filepath. I think I should just experiment more. Thanks! – Luke Villanueva Jul 10 '12 at 15:12
  • @SSA- Sir thank you again for the help. But the image is not displaying. There's a broken image sign I think it's either the conversion failed or the type issues. Can you help me with this? – Luke Villanueva Jul 11 '12 at 09:41
  • I think it has nothing to do with converting inputstream to byte array. Can you verify byte array has data and filelength is correct? – SSA Jul 11 '12 at 09:45
  • Yes, the byte array is being stored at the database. But when I'm retrieving it and loading it to my image tag. It shows broken image. – Luke Villanueva Jul 11 '12 at 09:49
  • how do you retrieve and assign it to image tag? http://www.codeproject.com/Articles/10861/Storing-and-Retrieving-Images-from-SQL-Server-usin – SSA Jul 11 '12 at 09:50
  • Here see this post http://stackoverflow.com/questions/11429925/failed-to-load-image-from-database – Luke Villanueva Jul 11 '12 at 09:57
-3
using (var stream = upload.InputStream)
{
    // use stream here: using StreamReader, StreamWriter, etc.
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433