HTTP File Upload without User Interaction using .NET
(Page 1 of 4 )
This article aims to teach users how to upload files by using .NET without getting messy, as can happen with HTTP. The author looks at possible solutions and explains why they do not work, then goes on to outline his solution.
Introduction
This article addresses the problem of uploading a file without user interaction, similar to an article I previously wrote, "HTTP File Download without user interaction using .NET". Most of the upload code available depends on the user to browse his or her hard disk to select the file (based on RFC1867), and it doesn’t display a progress bar while uploading it to the server. This article provides an appropriate solution for it.
Concept
The idea is to have an ActiveX control (for other alternatives, read the download article noted above) read the file from the client's hard disk and write it into the Request stream of HTTP. The ASP.NET code on the server will read the file content from the Request stream and write it into the hard disk.
About ActiveX Control
The term 'ActiveX' refers to an extension of existing OLE and COM technologies. An ActiveX control is an object that supports a customizable, programmatic interface. Using the methods, events, and properties exposed by a control, Web authors can automate their HTML pages. Examples of such controls include text boxes, command buttons, audio players, video players, stock tickers, and so on.
The following are the possible solutions which can be used to solve our problem.
- INET control - This is an ActiveX control provided by Microsoft for Internet transfer of files. We can read the file contents into a string and send it as POST data using the function Execute provided by the control. If all you want to do is upload the file, then this method should suffice, but if you want to display the progress as well, then this solution can't be used as it doesn’t provide the number of bytes being uploaded.
- Wininet dll - This DLL contains a set of APIs provided by Microsoft for Internet transfer of files. Performance is better in this case since we are directly using the APIs and not relying on another control to provide the functionality. This is the DLL which I used for downloading the file but sadly for uploading the file, it has a bug (it always returns 0 when it should return 1) in its InternetWriteFile function.
- Winsock control - This is an ActiveX control provided by Microsoft for network programming. It uses sockets to connect to the server and get the files. It’s a low level control where the programmer has to do the bulk of the work, but luckily it provides sendProgress event which gives information about the number of bytes uploaded. So we would be using this control to solve our problem.
Next: Solution >>
More .NET Articles
More By Chandru Prashanth