Lecture 9
Upload and Download Files
Upload file means sending data or a file from your computer to somewhere on the
Internet (Server). Download file: It means receiving data or a file from the Internet
(Server) on your computer, see figure1.
figure1
✓ Upload file means sending data or a file from your computer to somewhere on
the Internet (Server). If a site allows uploads, it will have an upload utility to
help perform the file transfer. Each site manages this process differently.
Usually, the site will have help pages to walk you through the upload process.
Many sites have an upload button that opens a dialog box.
The dialog box will appear assisting you to select a file from your computer.
Browse to the location where your file is stored and select it, then click the
Open button, see figure1. Afterward, a progress bar tracking the upload
process will appear on the page.
1
Figure1
✓ Download file: It means receiving data or a file from the Internet (Server) on
your computer. If you click the link or button, your browser should quick you to
select one of two methods for downloading the file.
• Open with will download the file and load it immediately in the
specified program.
• Save File will download it and save it to your hard drive.
2
➢ Different between "Uploading" and "downloading”
"Uploading" and "downloading" are terms used to refer to types of electronic data
transfers. The difference between them is the direction in which the files are being
transferred. Files are considered to be uploaded when they are transferred from a
computer or other electronic device to a central server, and downloading is when the
files are transferred from a server to a smaller peripheral unit, such as a
computer, smartphone or other device. These two different types of transfers are
often done via the Internet, such as when a file is downloaded from a website. The
transfer of data from one system or device to a similar system or device, such as
from a desktop computer to a laptop, usually is not considered uploading or
downloading.
❖ "Uploading" and "downloading” in [Link]
In [Link], Will explain how to upload file or image from local drive to website using
fileupload control. The fileupload control in [Link] used to upload any file like
image, document file, zip file .etc to [Link] website.
• Create Upload file in [Link]
Step 1 – Open the Visual Studio –> Create a new empty Web application.
Step 2 – Create a New web page for display FileUpload control.
Step 3 – Drag and drop FileUpload control on web page along with one
Button control from Toolbox.
Step 4 – write server-side code in button click event for upload image or any
file using FileUpload control.
3
o [Link] code for create FileUpload control on web page.
<asp:FileUpload ID="FileUpload1" runat="server" />
Now, add new folder for save uploaded image or any file by right click on website
project and select New Folder shows like below. give name to folder newly created
folder in [Link]. when we upload file or image using fileupload control the file
uploaded in to folder in your website.
[Link] FileUpload Control
4
Here, is the [Link] fileupload control example design layout. here we took one
fileupload control, a button control and a label control for design web page.
fileupload control brows and select the image or file from your local drive.
[Link] FileUpload Control
Here, we can see the output when we browse the file for upload. just select any file
from local drive and select open button to select the file for upload.
5
After selecting file for upload click the Upload button to upload file to desired
location at website. Here, is the button click event code for upload file to specific
folder on website.
Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace Upload_and_download_files
{
public partial class WebForm1 : [Link]
{
protected void Button1_Click(object sender, EventArgs e)
{
string fileName = [Link]([Link]);
[Link]([Link]("/Uploads/") + fileName);
[Link]([Link]);
}}}
o Create download file
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace Upload_and_download_files
{
public partial class WebForm1 : [Link]
{
protected void LinkButton1_Click(object sender, EventArgs e)
{
string fileName = "[Link]"; // This is file found in folder see figure below
[Link] = "application/octet-stream";
[Link]("Content-Disposition", "attachment; filename=" + fileName);
[Link]([Link]("/uploads/"+ fileName));
[Link](); } } }
6
Output: Click on LinkButton for download file