I do not know if it was a hurried reading, but I did not exactly understand your question. Anyway, I'm sending below the way I work with uploading files. My systems are in MVC, however it is easy to 'convert' the code below.
Class receiving file
[AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
public JsonResult UploadArquivo()
{
HttpPostedFileBase file = Request.Files[0] as HttpPostedFileBase;
if (file != null && file.ContentLength > 0)
{
var ftp = new Helpers.Ftp();
string nome = ftp.GerarNome(file.FileName.Substring(file.FileName.LastIndexOf(".")));
if (ftp.UploadArquivo(file, nome, "Web/arquivos/"))
return Json(new { Nome = nome }, JsonRequestBehavior.AllowGet);
else
return Json(new { Nome = "" }, JsonRequestBehavior.AllowGet);
}
return Json(new { Nome = "" }, JsonRequestBehavior.AllowGet);
}
Upload File
public Boolean UploadArquivo(HttpPostedFileBase file, string nome, string diretorio)
{
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp+diretorio + nome);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(usuario, senha);
request.UsePassive = false;
request.Proxy = null;
byte[] fileContents = new byte[file.ContentLength];
file.InputStream.Position = 0;
file.InputStream.Read(fileContents, 0, file.ContentLength);
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
return true;
}
catch
{
return false;
}
}
Ftp Definition
private string ftp;
private string usuario;
private string senha;
public Ftp()
{
ftp = "";
usuario = "";
senha = "";
}
File name generator
public string GerarNome(string extensao)
{
return DateTime.Now.ToString().Replace(":", "").Replace("/", "").Replace(" ", "") + extensao;
}
With this code you can handle the entire upload process.
In this case, all classes with the exception of JsonResult UploadArquivo()
are within:
namespace Ui.Web.Helpers
{
public class Ftp{
// Código
}
}
I do not know if the answer helped, if you edit your question with further instructions. I edit the answer.