I don’t know if it was a hasty read, but I didn’t quite understand your question. Anyway, I’m sending down the way I work with uploading files. My systems are in MVC, but it is easy to 'convert' the code below.
Class that receives 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);
}
Definition of the Uploadfile
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 setting
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 whole upload process.
In this case, all classes except JsonResult UploadArquivo()
are inside:
namespace Ui.Web.Helpers
{
public class Ftp{
// Código
}
}
I don’t know if the answer helped, if you edit your question with further instructions. I edit the answer.
I don’t understand what you want. You are reading the binary file correctly. I imagine you are talking about the
WebClient.UploadFile
, it is used to send a file, not to receive.– Maniero
This method is correct, but the path File we could not catch by Uploadfile more... If I run this method by taking (Uploadfile.PostedFile.Filename) from Notfound. Now you understand?
– Lucas Vasconcelos
But what is the doubt?
– Maniero
The method that Uploadfile has to get information about the file is this (Uploadfile.PostedFile.Filename), but it only brings the name + file extension. Ai da notfound... that’s my problem.
– Lucas Vasconcelos
Show me how you’re using it.
– Maniero
@bigown I updated the topic explaining better.
– Lucas Vasconcelos