Reputation: 611
I am uploading file in new thread, but looking like server is taking it in same main thread. (using ASP.NET MVC)
Locally it takes 3Second for file uploading, but on live web server it is taking 20seconds to complete method and that is just because of file uploading. i am unable to understand that it is in background thread then why it is adding time in returning.
On live web server I am using IIS 8. Following is the code
string path = string.Empty;
string thumbPath = string.Empty;
HttpContext cont = HttpContext.Current;
string url = HttpContext.Current.Request.Url.Host.ToLower();
Thread t1= new Thread(() =>
{
if (files.Count() > 0 || files != null)
{
foreach (HttpPostedFileBase file in files)
{
if (file != null)
{
string newFileName = System.Guid.NewGuid().ToString("N");
Photo photo = new Photo();
path = UploadFile(cont, file, ad.Id.ToString(), newFileName);
//Create thumbnail
thumbPath = CreateAdThumbNail(cont, file, ad.Id.ToString(), newFileName);
}
}
}
});
t1.IsBackground = true;
t1.Start();
string[] message = new string[2];
message[0] = "true";
message[1] = "http://" + HttpContext.Current.Request.Url.Host.ToLower() + "/Ad/Item/" + "?section=myads";
return Json(message, JsonRequestBehavior.AllowGet);
Upvotes: 1
Views: 1136
Reputation: 23690
I think that you're expecting your upload to the server to happen in another thread and it won't because it's performing an actual HTTP request and sending the data to your server. I assume that it is the upload that is taking the time and not the logic within the new thread, the speed of the HTTP request is dependent on the connection speed between the server and the uploading party and the speed at which your server can store the information.
What you're doing in your additional thread is just the final processing of the file that has been sent to the server - moving it somewhere else on the file system and creating a thumbnail of the uploaded image. By the time you hit your new thread creation, the file has already been sent to the server.
If you're not doing this already, I suggest sending the upload request using an AJAX request which will execute asynchronously 'behind the scenes' and won't leave your users looking at a frozen screen whilst a 20 second upload is taking place.
The problem is that Ajax uploads require an iFrame
and all sorts of fancy techniques to ensure that it is compatible with all browsers. I wouldn't bother doing this yourself unless you want to, but I use the following plugin for all of the uploads on my site and it works like a charm: http://malsup.com/jquery/form/
The method I use is ajaxSubmit()
which submits your form using AJAX along with any <input type="file" />
fields.
Upvotes: 1