Reputation: 71
I want to upload image by using ftp account. My code is like this. But when I choose image and submit, It says me "Could not find file 'C:\Program Files (x86)\IIS Express\picture.jpg'." I know my image is on my desktop and I choose from there. If I copy my image manually this IIS folder, it will upload but this is not sensible. I must choose my image where I want. But it is looking for in IIS Express folder.
[HttpPost, ValidateInput(false)]
public ActionResult Insert(Press model, HttpPostedFileBase uploadfile)
{
...........
...........
...........
...........
if (uploadfile.ContentLength > 0)
{
string fileName = Path.Combine(uploadfile.FileName);
var fileInf = new FileInfo(fileName);
var reqFtp =
(FtpWebRequest)
FtpWebRequest.Create(
new Uri("ftp://ftp.adres.com" + fileInf.Name));
reqFtp.Credentials = new NetworkCredential(username, password);
reqFtp.KeepAlive = false;
reqFtp.Method = WebRequestMethods.Ftp.UploadFile;
reqFtp.UseBinary = true;
reqFtp.ContentLength = uploadfile.ContentLength;
int bufferlength = 2048;
byte[] buff = new byte[bufferlength];
int contentLen;
FileStream fs = fileInf.OpenRead();
try
{
Stream strm = reqFtp.GetRequestStream();
contentLen = fs.Read(buff, 0, bufferlength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, bufferlength);
}
strm.Close();
fs.Close();
}
catch (Exception ex)
{
}
}
...........
...........
...........
...........
return View();
}
}
Upvotes: 0
Views: 8705
Reputation: 11
How to upload file by ftp using Asp.net MVC.
View
<form method="post" enctype="multipart/form-data">
<input type="file" id="postedFile" name="postedFile" />
<input type="submit" value="send" />
</form>
Controller ActionResult
[HttpPost]
public ActionResult Index(HttpPostedFileBase postedFile)
{
//FTP Server URL.
string ftp = "ftp://ftp.YourServer.com/";
//FTP Folder name. Leave blank if you want to upload to root folder.
string ftpFolder = "test/";
byte[] fileBytes = null;
string ftpUserName = "YourUserName";
string ftpPassword = "YourPassword";
//Read the FileName and convert it to Byte array.
string fileName = Path.GetFileName(postedFile.FileName);
using (BinaryReader br = new BinaryReader(postedFile.InputStream))
{
fileBytes = br.ReadBytes(postedFile.ContentLength);
}
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
//Enter FTP Server credentials.
request.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
request.ContentLength = fileBytes.Length;
request.UsePassive = true;
request.UseBinary = true;
request.ServicePoint.ConnectionLimit = fileBytes.Length;
request.EnableSsl = false;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileBytes, 0, fileBytes.Length);
requestStream.Close();
}
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
return View();
}
For upload large files you can add this line to your web.config Thanks to Karthik Ganesan
<httpRuntime maxRequestLength="whatever value you need in kb max value is 2,147,483,647 kb" relaxedUrlToFileSystemMapping="true" />
under system.web section the default is 4 mb size limit
Upvotes: 1
Reputation: 71
I found a solution for my problem and I want to share here, maybe a person can benefit
void UploadToFtp(HttpPostedFileBase uploadfile)
{
var uploadurl = "ftp://ftp.adress.com/";
var uploadfilename = uploadfile.FileName;
var username = "ftpusername";
var password = "ftppassword";
Stream streamObj = uploadfile.InputStream;
byte[] buffer = new byte[uploadfile.ContentLength];
streamObj.Read(buffer, 0, buffer.Length);
streamObj.Close();
streamObj = null;
string ftpurl = String.Format("{0}/{1}", uploadurl, uploadfilename);
var requestObj = FtpWebRequest.Create(ftpurl) as FtpWebRequest;
requestObj.Method = WebRequestMethods.Ftp.UploadFile;
requestObj.Credentials = new NetworkCredential(username, password);
Stream requestStream = requestObj.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Flush();
requestStream.Close();
requestObj = null;
}
Upvotes: 6
Reputation: 188
Ensure the value in fileName is what you are expecting here:
string fileName = Path.Combine(uploadfile.FileName);
You most likely need to pass the path as a string, as well as the filename into the Combine method.
string fileName = Path.Combine(varFilePath, uploadfile.FileName);
Path.Combine expects an array of strings to combine: https://msdn.microsoft.com/en-us/library/system.io.path.combine%28v=vs.110%29.aspx
Upvotes: 2