How to Download Files That Have Been Uploaded to My Server
ASP.NET on .NET Framework 3.5 Service Pack 1 More...Less
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.NET. You lot can download the sample packages from the following download icons. Both C# and Visual Bones .Net language versions of the sample bundle are bachelor.
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. Also, this code sample uses Uniform Resource Identifiers (URIs) to identify the locations of files on a server. The key classes used in this lawmaking sample are the WebClient course and the WebRequest class.
Difficulty level
Download information
To download this code sample, click one of the post-obit links:
Technical overview
It 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 class is a high-level form that makes server interactions easier. WebRequest objects are used by the WebClient class to make requests. The HttpWebRequest and FtpWebRequest classes are protocol-specific implementations of the abstract WebRequest class. HttpWebRequest implements the GET 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 code sample uses the UploadData and DownloadData methods of the WebClient form 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-form-urlencoded" Internet media type. The DownloadData method is used with a FileStream object to store the incoming data stream and write the byte array to a local file.
Sample overview
In this sample code, y'all will find the RemoteFileForm.aspx file that explains how to use the following two new classes:
-
RemoteUpload
-
RemoteDownload
The RemoteUpload class
The RemoteUpload course has two child classes. These classes are HttpRemoteUpload and FtpRemoteUpload. Both classes utilise the RemoteUpload constructor. The RemoteUpload class requires a byte assortment of the file data and a server URI. You tin can as well specify the proper noun to employ for the uploaded file. The FtpRemoteUpload grade uses FtpWebRequest directly (instead of using the higher-level WebClient course) to handle the specific requirements of the FTP protocol.
See the following course definitions for more information about the RemoteUpload class:
public class HttpRemoteUpload : RemoteUpload
{
public HttpRemoteUpload(byte[] fileData, string fileNamePath, string urlString)
: base(fileData, fileNamePath, urlString)
{
}
public override bool UploadFile()
{
byte[] postData;
try
{
postData = this.FileData;
using (WebClient customer = new WebClient())
{
client.Credentials = CredentialCache.DefaultCredentials;
client.Headers.Add("Content-Type", "application/x-world wide web-form-urlencoded");
client.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 = truthful;
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(buff, 0, buffLength);
while (contenctLength > 0)
{
strm.Write(vitrify, 0, contenctLength);
contenctLength = ms.Read(buff, 0, buffLength);
}
}
render 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.
Note If you do not specify a file name to use to store the file on the server, the system volition automatically generate a file name co-ordinate to the current date and fourth dimension on the server. The date and fourth dimension is accurate to the millisecond. Later the UploadData method finishes, the result is shown on the current page.
The RemoteDownload class
The RemoteDownload class also has two child classes. These classes are HttpRemoteDownload and FtpRemoteDownload. The RemoteDownload grade requires a URI resource and a local concrete directory. The RemoteDownload grade checks to make certain that the URI resource 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 grade uses FtpWebRequest directly (instead of using the higher-level WebClient class) to handle the specific requirements of the FTP protocol.
Run across the following grade definitions for more information about the RemoteDownload class:
public class HttpRemoteDownload : RemoteDownload
{
public HttpRemoteDownload(string urlString, string descFilePath)
: base(urlString, descFilePath)
{
}
public override bool DownloadFile()
{
cord fileName = System.IO.Path.GetFileName(this.UrlString);
string descFilePathAndName =
System.IO.Path.Combine(this.DescFilePath, fileName);
try
{
WebRequest myre = WebRequest.Create(this.UrlString);
}
take hold of
{
render false;
}
try
{
byte[] fileData;
using (WebClient client = new WebClient())
{
fileData = customer.DownloadData(this.UrlString);
}
using (FileStream fs =
new FileStream(descFilePathAndName, FileMode.OpenOrCreate))
{
fs.Write(fileData, 0, fileData.Length);
}
render true;
}
grab (Exception ex)
{
throw new Exception("download field", ex.InnerException);
}
}
}
public grade FtpRemoteDownload : RemoteDownload
{
public FtpRemoteDownload(string urlString, string descFilePath)
: base(urlString, descFilePath)
{
}
public override bool DownloadFile()
{
FtpWebRequest reqFTP;
string fileName = Arrangement.IO.Path.GetFileName(this.UrlString);
string descFilePathAndName =
Organisation.IO.Path.Combine(this.DescFilePath, fileName);
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(this.UrlString);
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
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 truthful;
}
catch (Exception ex)
{
throw new Exception("upload failed", ex.InnerException);
}
}
}
Note For more information nearly how to create and deploy the sample application, see the Readme.txt file that is included in the download packet.
Languages
This lawmaking sample is available in the following programming languages:
Language | Projection Name |
---|---|
Visual C# | CSRemoteUploadAndDownload |
Visual Bones.NET | VRemoteUploadAndDownload |
Technology Category
-
ASP.NET two.0
-
ASP.Cyberspace 3.5
-
ASP.NET 4.0
References
For more data most the WebClient form, visit the following Microsoft Developer (MSDN) website:
General information near the WebClient classFor more data about the UploadData method, visit the following MSDN website:
General information about the UploadData methodFor more data well-nigh the DonwloadData method, visit the following MSDN website:
Full general data most the DonwloadData methodFor more information about the FtpWebRequest method, visit the following MSDN website:
Full general data about the FtpWebRequest methodFor more information about how to upload Files with FTP, visit the following MSDN website:
How to upload Files with FTP
More Information
What is All-In-One Code Framework?
All-In-One Code Framework shows most Microsoft development techniques by using code samples in different programming languages. Each example is carefully selected, equanimous, and documented to show one common lawmaking scenario. For more data about All-In-One Code Framework, visit the following Microsoft website:
http://1code.codeplex.com
How to find more than All-In-1 Code Framework samples
To find more All-In-One Code Framework samples, search for "kbcodefx" together with related keywords on the Microsoft support Web site. Or, visit the post-obit Microsoft website:
All-In-I Code Framework samples
Rapid publishing disclaimer
Microsoft corporation and/or its respective suppliers brand no representations about the suitability, reliability, or accuracy of the information and related graphics independent herein. All such information and related graphics are provided "as is" without warranty of any kind. Microsoft and/or its respective suppliers hereby disclaim all warranties and conditions with regard to this data and related graphics, including all implied warranties and conditions of merchantability, fitness for a item purpose, workmanlike endeavour, title and non-infringement. Y'all specifically agree that in no event shall Microsoft and/or its suppliers be liable for whatever straight, indirect, castigating, incidental, special, consequential damages or whatever amercement whatsoever including, without limitation, damages for loss of apply, information or profits, arising out of or in whatsoever way connected with the utilize of or inability to use the information and related graphics contained herein, whether based on contract, tort, negligence, strict liability or otherwise, even if Microsoft or any of its suppliers has been advised of the possibility of damages.
mcphersonthismillond.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 "How to Download Files That Have Been Uploaded to My Server"
Post a Comment