I am currently developing an application for a client that will use the administration panel for uploading images to the picture gallery and some other scanned documents to the database. I have successfully written the controller action for saving and retrieving the files saved in the database.
Now I am stuck on how to display a progress bar when the file is uploading to the database. I have tried several controls, like jQuery's uploadify and some others. They are all successful in saving the file to a folder albeit not while uploading to MS-SQL Database.
Here's a sample of my Model:
public Class myImages
{
[Key, Required]
public string ImageID { get; set; }
[Required, MaxLength(100), DisplayName("Image Title:")]
public string ImageTitle { get; set; }
[DisplayName("Image File:")]
public byte[] ImageData { get; set; }
public string ImageContentType { get; set; }
}
I have created a function in infrastructure for returning a byte array of the uploaded file, here's the code:
public byte[] uploadedFileToByteArray(HttpPostedFileBase file)
{
int nFileLen = file.ContentLength;
byte[] result = new byte[nFileLen];
file.InputStream.Read(result, 0, nFileLen);
return result;
}
...And here's the Control Action for Create
[HttpPost, ValidateInput(false), ValidateAntiForgeryToken]
public ActionResult Create(myImage myimage)
{
if (ModelState.IsValid)
{
myimage.ImageID = DateTime.Now.Ticks.ToString();
myimage.ImageData = uploadedFileToByteArray(Request.Files[0]);
myimage.ImageContentType = Request.Files[0].ContentType;
db.Pictures.Add(picture);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(picture);
}
As I have said, the above code is working perfectly fine, alas I cannot seem to get the progress bar working while the file is uploading. Any ideas how to do it?
I have also tried uploading the file to a temp folder and then reading the bytes of that file, then uploading to the SQL Server, this approach works, but for some reason my client does not want that and I am also not satisfied with this solution.
I have also noticed that when uploading the file, Google's chrome shows the upload percent, if the browser can access the data at client side, is there a way to access the uploaded/sent bytes via jQuery or Ajax to display a progress bar while uploading?
I am using Visual Studio 2010, ASP.NET, MVC 3.