Provides a Way to Upload and Download Files From an Internet-based Server to a Computer on a Lan.

INTRODUCTION

This commodity describes an All-In-One framework sample that is available for download. This code sample demonstrates how to upload and download files from a remote or local server in ASP.Cyberspace. You can download the sample packages from the post-obit download icons. Both C# and Visual Bones .NET language versions of the sample package are available.

This code sample demonstrates how to upload and download files from a server that is not in the scope of the user's request domain. Functionality is provided for transferring files with both the HTTP and FTP protocols. As well, this code sample uses Compatible Resources Identifiers (URIs) to place the locations of files on a server. The fundamental classes used in this code sample are the WebClient class and the WebRequest class.

Difficulty level

Download information

To download this code sample, click 1 of the following links:

Technical overview

Information technology is fairly easy to upload and download files from a remote server in ASP.NET. The .Net Framework class library provides some lightweight asking objects. The WebClient form is a high-level class that makes server interactions easier. WebRequest objects are used past the WebClient class to make requests. The HttpWebRequest and FtpWebRequest classes are protocol-specific implementations of the abstract WebRequest class. HttpWebRequest implements the Become and POST methods of the HTTP protocol to upload and download files. FtpWebRequest implements the STOR and RETR methods of the FTP protocol to upload and download files.

This lawmaking sample uses the UploadData and DownloadData methods of the WebClient class to transfer data to and from a remote server URI. The UploadData method is used with the HTTP protocol's PUT method and the "application/10-www-grade-urlencoded" Internet media type. The DownloadData method is used with a FileStream object to shop the incoming information stream and write the byte array to a local file.

Sample overview

In this sample lawmaking, you will find the RemoteFileForm.aspx file that explains how to utilize the post-obit 2 new classes:

  • RemoteUpload

  • RemoteDownload

The RemoteUpload class

The RemoteUpload form has two child classes. These classes are HttpRemoteUpload and FtpRemoteUpload. Both classes use the RemoteUpload constructor. The RemoteUpload class requires a byte assortment of the file information and a server URI. You can also specify the name to use for the uploaded file. The FtpRemoteUpload grade uses FtpWebRequest direct (instead of using the higher-level WebClient course) to handle the specific requirements of the FTP protocol.

See the post-obit form definitions for more than information about the RemoteUpload course:
public class HttpRemoteUpload : RemoteUpload
{
public HttpRemoteUpload(byte[] fileData, string fileNamePath, string urlString)
: base of operations(fileData, fileNamePath, urlString)
{

}

public override bool UploadFile()
{
byte[] postData;
endeavour
{
postData = this.FileData;
using (WebClient client = new WebClient())
{
client.Credentials = CredentialCache.DefaultCredentials;
client.Headers.Add together("Content-Type", "awarding/x-www-form-urlencoded");
customer.UploadData(this.UrlString, "PUT", postData);
}

render true;
}
catch (Exception ex)
{
throw new Exception("Failed to upload", ex.InnerException);
}

}
}

public class FtpRemoteUpload : RemoteUpload
{
public FtpRemoteUpload(byte[] fileData, string fileNamePath, string urlString)
: base(fileData, fileNamePath, urlString)
{

}

public override bool UploadFile()
{
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(this.UrlString);
reqFTP.KeepAlive = true;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = this.FileData.Length;

int buffLength = 2048;
byte[] vitrify = new byte[buffLength];
MemoryStream ms = new MemoryStream(this.FileData);

try
{
int contenctLength;
using (Stream strm = reqFTP.GetRequestStream())
{
contenctLength = ms.Read(vitrify, 0, buffLength);
while (contenctLength > 0)
{
strm.Write(vitrify, 0, contenctLength);
contenctLength = ms.Read(buff, 0, buffLength);
}
}

return truthful;
}
grab (Exception ex)
{
throw new Exception("Failed to upload", ex.InnerException);
}
}

} In the RemoteFileForm.cs file, when you click the Upload button, an instance of the RemoteUpload object is created. Laissez passer the server URI and a local physical file path as parameters to create the object.

Notation If you practice not specify a file proper noun to use to store the file on the server, the organisation volition automatically generate a file name co-ordinate to the current date and time on the server. The engagement and time is accurate to the millisecond. Afterwards the UploadData method finishes, the outcome is shown on the current page.

The RemoteDownload form

The RemoteDownload class also has two child classes. These classes are HttpRemoteDownload and FtpRemoteDownload. The RemoteDownload grade requires a URI resource and a local physical directory. The RemoteDownload class checks to make sure that the URI resources exists earlier the download is started. The class retrieves the stream that contains the response data from the server, and then writes this byte assortment to a FileStream. The FtpRemoteDownload form uses FtpWebRequest directly (instead of using the college-level WebClient class) to handle the specific requirements of the FTP protocol.

Come across the post-obit course definitions for more information almost the RemoteDownload class:
public form HttpRemoteDownload : RemoteDownload
{
public HttpRemoteDownload(string urlString, string descFilePath)
: base(urlString, descFilePath)
{

}

public override bool DownloadFile()
{
string fileName = Organization.IO.Path.GetFileName(this.UrlString);
string descFilePathAndName =
System.IO.Path.Combine(this.DescFilePath, fileName);
attempt
{
WebRequest myre = WebRequest.Create(this.UrlString);
}
take hold of
{
return false;
}
try
{
byte[] fileData;
using (WebClient client = new WebClient())
{
fileData = client.DownloadData(this.UrlString);
}
using (FileStream fs =
new FileStream(descFilePathAndName, FileMode.OpenOrCreate))
{
fs.Write(fileData, 0, fileData.Length);
}
return true;
}
catch (Exception ex)
{
throw new Exception("download field", ex.InnerException);
}
}
}

public class FtpRemoteDownload : RemoteDownload
{
public FtpRemoteDownload(string urlString, string descFilePath)
: base(urlString, descFilePath)
{

}

public override bool DownloadFile()
{
FtpWebRequest reqFTP;

string fileName = System.IO.Path.GetFileName(this.UrlString);
cord descFilePathAndName =
Arrangement.IO.Path.Combine(this.DescFilePath, fileName);

attempt
{

reqFTP = (FtpWebRequest)FtpWebRequest.Create(this.UrlString);
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = truthful;

using (FileStream outputStream = new FileStream(descFilePathAndName, FileMode.OpenOrCreate))
using (FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse())
using (Stream ftpStream = response.GetResponseStream())
{
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
}
render true;
}

take hold of (Exception ex)
{
throw new Exception("upload failed", ex.InnerException);
}
}
}
Note For more data about how to create and deploy the sample application, run across the Readme.txt file that is included in the download parcel.

Languages

This code sample is bachelor in the following programming languages:

Language

Projection Name

Visual C#

CSRemoteUploadAndDownload

Visual Basic.Net

VRemoteUploadAndDownload

Technology Category

  • ASP.Net 2.0

  • ASP.Internet 3.5

  • ASP.NET 4.0

References

For more information about the WebClient grade, visit the following Microsoft Developer (MSDN) website:

General information about the WebClient classFor more information almost the UploadData method, visit the following MSDN website:

Full general information virtually the UploadData methodFor more than information well-nigh the DonwloadData method, visit the following MSDN website:

General information well-nigh the DonwloadData methodFor more than data about the FtpWebRequest method, visit the following MSDN website:

General information about the FtpWebRequest methodFor more information well-nigh how to upload Files with FTP, visit the following MSDN website:

How to upload Files with FTP

More Data

What is All-In-One Lawmaking Framework?

All-In-One Lawmaking Framework shows nigh Microsoft evolution techniques by using code samples in different programming languages. Each instance is carefully selected, equanimous, and documented to show one common lawmaking scenario. For more information near All-In-One Code Framework, visit the post-obit Microsoft website:

http://1code.codeplex.com

How to find more All-In-One Code Framework samples

To find more than All-In-One Code Framework samples, search for "kbcodefx" together with related keywords on the Microsoft support Web site. Or, visit the following Microsoft website:

All-In-One Code Framework samples

Rapid publishing disclaimer

Microsoft corporation and/or its respective suppliers make no representations almost the suitability, reliability, or accuracy of the information and related graphics contained herein. All such information and related graphics are provided "every bit is" without warranty of any kind. Microsoft and/or its corresponding suppliers hereby disclaim all warranties and weather with regard to this information and related graphics, including all unsaid warranties and conditions of merchantability, fettle for a particular purpose, workmanlike effort, title and not-infringement. You specifically agree that in no event shall Microsoft and/or its suppliers exist liable for any direct, indirect, punitive, incidental, special, consequential damages or any damages whatsoever including, without limitation, amercement for loss of use, data or profits, arising out of or in whatever mode connected with the employ of or disability to use the information and related graphics contained herein, whether based on contract, tort, negligence, strict liability or otherwise, fifty-fifty if Microsoft or any of its suppliers has been brash of the possibility of amercement.

Demand more than assist?

robinsontworet.blogspot.com

Source: https://support.microsoft.com/en-us/topic/how-to-upload-and-download-files-from-a-remote-server-in-asp-net-75254f9c-9dfa-f50f-01d8-31241160445c

0 Response to "Provides a Way to Upload and Download Files From an Internet-based Server to a Computer on a Lan."

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel