Asp.net mvc upload file to server
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. I have created a function in infrastructure for returning a byte array of the uploaded file, here's the code:. 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.
You need to read the bytes into memory in chunks, updating the amount of data read in some server-side variable in the session, perhaps that can be accessed by the upload plugin you are using to measure progress.
As it is you read all of the data in one go and there is no way to report partial progress. Use another action to report back the amount read so far for the progress indicator. Note, you might need to use an asynchronous controller to make this work. Note this is just for an idea. It would need quite a bit of work to be robust, handle multiple files, etc. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 10 years, 3 months ago. SaveAs path ;. This newly created view will be added under Views folder like as shown below. Now open that view and write the code like as shown below. Newer Post Older Post Home. Subscribe to: Post Comments Atom. Interview Questions in ASP. NET Framework Simple login form example in asp. Search This Site. Get Latest articles in your inbox for free.
Enter your email address:. Tags Asp. Net General VB. All Rights Reserved. One of those is the FileUpload, and its absence seems to cause a few problems. This article looks at how to upload files to the server in an MVC world, and how to get them back from the server to the user again.
In Web Forms, when you drag a FileUpload control on to the designer, something happens when the page is rendered which you probably don't notice. Within an MVC View, there are a number of ways to set this up. The first is with HTML:. This is needed because the form by default will be submitted via the HTTP get method. The following approach, using the Html.
BeginForm extension method renders the exact same html when the page is requested:. We'll come back to that shortly. In the meantime, the resulting page should look rather blandly like this:. So we can now browse to a local file and click the submit button to upload it to the web server. What is needed next is some way to manage the file on the server.
When using a FileUpload control, you generally see code that checks to see if a file actually has been uploaded, using the FileUpload. HasFile method. However, a quick extension method can take care of that:. When you look at Controller class, you see that it has a Request object as a property, which is of type HttpRequestBase. Each item within the collection is of type HttpPostedFileBase. The extension method checks the item to make sure there's one there, and that it has some content.
Essentially, this is identical to the way that the FileUpload. HasFile method works.
0コメント