http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2 shows how to upload files. Now I would like an example where I can display
the progress of the file upload using ProgressMessageHandler. So far I was able to find examples of the class being used in WPF or console application, but I would like to use it either on the MVC controller method or the WebAPI method and show the upload
progress to JQuery UI progress bar. Can someone post the sample code of how it can be done?
its pretty simple now that the controller can read the file async.
the client needs to pass a transactionid with the upload. you can use a form field, but the url parameter makes it avaliable at the start of the controller action.
the controller then stores status in a global store aviable to all requests. use transactionid as a key, and store length of the request, and number of bytes read. you can see see that the controller can read the stream async (you don;t want to wait until
the whole file has been read), and update the status.
the controller supplies a second action that given a transactionid returns the status.
now client side you start an async file upload via the fileapi if html 5 or a form post via an iframe (google for lots of examples). after kicking off upload, the client starts polling the server for the status (could use a comet approach like signalr here).
I know this isn't a timely answer but google & bing kept bringing me here on a similar search. I thought I would add my problem and solution in case someone else is in a similar situation.
I wanted to hook up progress events for file uploads in an mvc application using web api. I also came across many examples of using the ProgressMessageHandler but none in an MVC application. I didn't initially realize that the ProgressMessageHandler needed
to be hooked up at application startup and was trying to hook it up in the api controller itself. *BONK*
Anyways, its a simple matter of adding a ProgressMessageHandler to the MessageHandlers collection of the web api's HttpConfiguration. You can do this in your your global Application_Start() method or, if you are using the default MVC 4 template, in the WebApiConfig
class that sits in the App_Start directory. That is where I stuck it.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
ProgressMessageHandler progress = new ProgressMessageHandler();
progress.HttpSendProgress += new EventHandler<HttpProgressEventArgs>(HttpSendProgress);
config.MessageHandlers.Add(progress);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
private static void HttpSendProgress(object sender, HttpProgressEventArgs e)
{
HttpRequestMessage request = sender as HttpRequestMessage;
// Do something with the event
// e.ProgressPercentage, e.TotalBytes, e.BytesTransferred
}
}
Now that the event is firing you'll still have to find a way to return this to the client and update a progress bar or something similar. I've used signalr for this successfully but haven't tested it at scale. There are potentially many events and i would
guess that you would want to throttle what you send back down to the client (After all, they are trying to upload a file or files).
I believe it will be much efficeint if you send the file in chunks from client to server using html 5 chunk method supported by modern browsers. PlUpload makes it easy to use this with ProgressBar and fallback to flash/silverlight/html 4. Recently, Rick
put an example of plUpload with ASP.NET at
here.
Another way might be to read the input stream in http handler in chunks(Response.BufferOutput = false) and Flash out(using Response.Flush) the uploaded information to client, so that client can show the progress.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
I agree, there is no doubt that letting the client deliver progress messages through [flash/silverlight/html5 upload api] is more efficient. In fact, I use plupload as my default upload plugin.
The OP asked a very specific question regarding web api and ProgressMessageHandler, just answering it...
aspnetmvcweb...
0 Points
1 Post
WebAPI and Fileupload with ProgressMessageHandler
Aug 19, 2012 01:09 AM|LINK
http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2 shows how to upload files. Now I would like an example where I can display the progress of the file upload using ProgressMessageHandler. So far I was able to find examples of the class being used in WPF or console application, but I would like to use it either on the MVC controller method or the WebAPI method and show the upload progress to JQuery UI progress bar. Can someone post the sample code of how it can be done?
RickBlokdijk
Member
2 Points
3 Posts
Re: WebAPI and Fileupload with ProgressMessageHandler
Nov 10, 2012 08:25 AM|LINK
Hi, Already found a tutorial for this? looking for the same, could not find any examples.
Only the info in http://www.strathweb.com/2012/08/asp-net-web-api-is-released-whats-new-in-web-api-rtm-and-how-to-use-it/
Anyone got a web solution example?
bruce (sqlwo...
All-Star
36836 Points
5443 Posts
Re: WebAPI and Fileupload with ProgressMessageHandler
Nov 10, 2012 07:39 PM|LINK
its pretty simple now that the controller can read the file async.
the client needs to pass a transactionid with the upload. you can use a form field, but the url parameter makes it avaliable at the start of the controller action.
the controller then stores status in a global store aviable to all requests. use transactionid as a key, and store length of the request, and number of bytes read. you can see see that the controller can read the stream async (you don;t want to wait until the whole file has been read), and update the status.
the controller supplies a second action that given a transactionid returns the status.
now client side you start an async file upload via the fileapi if html 5 or a form post via an iframe (google for lots of examples). after kicking off upload, the client starts polling the server for the status (could use a comet approach like signalr here).
flavorful
Member
94 Points
22 Posts
Re: WebAPI and Fileupload with ProgressMessageHandler
Mar 12, 2013 11:32 PM|LINK
I know this isn't a timely answer but google & bing kept bringing me here on a similar search. I thought I would add my problem and solution in case someone else is in a similar situation.
I wanted to hook up progress events for file uploads in an mvc application using web api. I also came across many examples of using the ProgressMessageHandler but none in an MVC application. I didn't initially realize that the ProgressMessageHandler needed to be hooked up at application startup and was trying to hook it up in the api controller itself. *BONK*
Anyways, its a simple matter of adding a ProgressMessageHandler to the MessageHandlers collection of the web api's HttpConfiguration. You can do this in your your global Application_Start() method or, if you are using the default MVC 4 template, in the WebApiConfig class that sits in the App_Start directory. That is where I stuck it.
public static class WebApiConfig { public static void Register(HttpConfiguration config) { ProgressMessageHandler progress = new ProgressMessageHandler(); progress.HttpSendProgress += new EventHandler<HttpProgressEventArgs>(HttpSendProgress); config.MessageHandlers.Add(progress); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } private static void HttpSendProgress(object sender, HttpProgressEventArgs e) { HttpRequestMessage request = sender as HttpRequestMessage; // Do something with the event // e.ProgressPercentage, e.TotalBytes, e.BytesTransferred } }Now that the event is firing you'll still have to find a way to return this to the client and update a progress bar or something similar. I've used signalr for this successfully but haven't tested it at scale. There are potentially many events and i would guess that you would want to throttle what you send back down to the client (After all, they are trying to upload a file or files).
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: WebAPI and Fileupload with ProgressMessageHandler
Mar 13, 2013 07:07 AM|LINK
I believe it will be much efficeint if you send the file in chunks from client to server using html 5 chunk method supported by modern browsers. PlUpload makes it easy to use this with ProgressBar and fallback to flash/silverlight/html 4. Recently, Rick put an example of plUpload with ASP.NET at here.
Another way might be to read the input stream in http handler in chunks(Response.BufferOutput = false) and Flash out(using Response.Flush) the uploaded information to client, so that client can show the progress.
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
flavorful
Member
94 Points
22 Posts
Re: WebAPI and Fileupload with ProgressMessageHandler
Mar 13, 2013 08:43 AM|LINK
I agree, there is no doubt that letting the client deliver progress messages through [flash/silverlight/html5 upload api] is more efficient. In fact, I use plupload as my default upload plugin.
The OP asked a very specific question regarding web api and ProgressMessageHandler, just answering it...