Click here to Skip to main content
15,900,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Helo,

I'm developing an ASP.NET MVC3 application. I defined the file in form. In that file I browse for an image file. By using AJAX how can we pass image file content. If anyone knows please help me am searching for so many days in internet i will not find any solution.

Thanks And Regards
Prasanna Kumari
Posted
Updated 24-May-12 22:28pm
v2

1 solution

I've used a couple of async file upload solutions. I think this one[^] is pretty nice and easy to implement using MVC.


Add the following code to your controller :
C#
[Authorize]
[HttpPost]
public ActionResult Upload()
{
    bool continueUpload = true;
    var dyn = new { success=false,  error = "Unknown error while uploading" };

    string uploadPath = ConfigurationManager.AppSettings[AppSettingKeys.UPLOAD_REPOSITORY_SETTING];
    if (!string.IsNullOrEmpty(uploadPath))
    {
        string fileLocation = Path.Combine(uploadPath, Session.SessionID + "_" + Request.QueryString["qqfile"]);

        using (MemoryStream memStream = new MemoryStream())
        {
            try
            {
                Request.InputStream.CopyTo(memStream);
            }
            catch (HttpException hex)
            {
                continueUpload = false;
                dyn = new { success = false, error = "File is too large" };
            }
            if (continueUpload)
            {
                memStream.Seek(0, SeekOrigin.Begin);

                using (FileStream fstrm = new FileStream(fileLocation, FileMode.Append, FileAccess.Write, FileShare.None))
                {
                    memStream.CopyTo(fstrm);
                    dyn = new { success = true, error = string.Empty };
                }
            }
        }
    }

    return Json(dyn, "text/html");
}


This will handle your uploaded file(s). As you can see, the code above is not quite neat, so it needs a couple of tweaks, however it will work using the javascript library.


Hope it helps, good luck!
Eduard
 
Share this answer
 
v2
Comments
Prasanna Kumari 25-May-12 5:26am    
Hi Eduard,
Thanks For the reply,
But in my view i has to send that browse file to controller and from controller it saves it on the database....So my main aim is to send that browse file to the controller using ajax is it possible if possible please send me the solution...
Eduard Keilholz 25-May-12 5:50am    
The solution linked to is a javascript client side solution which uses AJAX to sent the file to the server. Your controller catches the incomming bytes and your can decide what to do with them (store them in DB, or as a file or whatever)
Prasanna Kumari 25-May-12 5:57am    
ok...So it passes the data in byte format to the controller
Eduard Keilholz 25-May-12 6:08am    
That's correct, see my modified answer...
Prasanna Kumari 25-May-12 6:59am    
Ya thanks
But i has to be saved to the database.So i think i dint need MemoryStream here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900