/// <summary> /// Uploads the specified file name. /// </summary> /// <param name="fileName">Name of the file.</param> public void Upload(string fileName) { tryAgain: FtpWebResponse response = null; try { Uri url = GetUrl(fileName); Console.WriteLine("Upload url: " + url); FtpWebRequest request = CreateRequest(url); request.Method = WebRequestMethods.Ftp.UploadFile; request.Credentials = new NetworkCredential(user, password); using (var requestStream = request.GetRequestStream()) using (var fileStream = File.Open(fileName, FileMode.Open)) { StreamPipe.Transfer(fileStream, requestStream, bufferSize); response = (FtpWebResponse)request.GetResponse(); Console.WriteLine(response.StatusDescription); Console.WriteLine("Upload complete."); } } catch (UriFormatException ex) { Console.WriteLine(ex.Message); } catch (IOException ex) { Console.WriteLine(ex.Message); } catch (WebException ex) { string status = ((FtpWebResponse)ex.Response).StatusDescription; if (status.Contains("550")) { if (MakeDirectorySafe()) { goto tryAgain; } } else { PrintWebException(ex); } } finally { if (response != null) { response.Close(); } } }
/// <summary> /// Downloads the specified URL. /// </summary> /// <param name="url">The URL.</param> /// <param name="fileName">Name of the file.</param> public void Download(string url, string fileName) { var request = AppCore.GameSettings.ServerSettings.CreateRequest(url); using (var response = (HttpWebResponse)request.GetResponse()) using (var source = response.GetResponseStream()) using (var destination = File.OpenWrite(fileName)) { StreamPipe.Transfer(source, destination); } }
/// <summary> /// Downloads the specified file name. /// </summary> /// <param name="fileName">Name of the file.</param> public void Download(string fileName) { try { Uri url = GetUrl(fileName); Console.WriteLine("Download url: " + url); FtpWebRequest request = CreateRequest(url); request.Method = WebRequestMethods.Ftp.DownloadFile; var downloadResponse = request.GetResponse(); using (var responseStream = downloadResponse.GetResponseStream()) using (var fileStream = File.Create(localdir + fileName)) { StreamPipe.Transfer(responseStream, fileStream, bufferSize); } } catch (Exception ex) { throw new FtpException("Error download file", ex); } }
private void AsyncCallback(IAsyncResult ar) { DownloadRequestCompleteEventArgs arg; var downloadRequest = (DownloadRequest)ar.AsyncState; try { using (var response = downloadRequest.Request.EndGetResponse(ar)) using (var source = response.GetResponseStream()) using (var destination = File.OpenWrite(downloadRequest.FileName)) { StreamPipe.Transfer(source, destination); } arg = new DownloadRequestCompleteEventArgs(downloadRequest.FileName, true, null); } catch (Exception ex) { arg = new DownloadRequestCompleteEventArgs(downloadRequest.FileName, false, ex); } if (Downloaded != null) { Downloaded(this, arg); } }
/// <summary> /// Sends the file. /// </summary> /// <param name="context">The context.</param> /// <param name="mode">The mode.</param> /// <param name="fileName">Name of the file.</param> /// <param name="content">The content.</param> /// <param name="mimeType">Type of the MIME.</param> public static void SendFile(this HttpContext context, SendMode mode, string fileName, Stream content, string mimeType) { PrepareResponse(context, mode, fileName, content.Length); StreamPipe.Transfer(content, context.Response.OutputStream); ResponseEnd(context.Response); }