i got a server host that only let me upload files through ftp (with code in my application).
so i got a code that works and lets me upload files to the server except with one strange thing, i can not upload files if me/user is logged in (authenticated). this is a shot in the dark to see if anybody maybe know why this is.
my error message is this
Access to the path 'D:\hshome\PATH' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.Directory.InternalCreateDirectory(String fullPath, String path, Object dirSecurityObj) at System.IO.Directory.CreateDirectory(String path) at BasicProject.Mvc.Areas.Account.Controllers.ProfileController.Test(TestViewModel viewModel)
my code i use for the upload is this
foreach (string item in Request.Files)
{
HttpPostedFileBase file = Request.Files[item];
if (file != null)
{
var path = "File";
var filePath = System.Web.HttpContext.Current.Server.MapPath("~/" + path + "/");
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://SITE/SITE/" + path + "/" + file.FileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("USER", "PASSWORD");
request.UsePassive = false;
var sourceStream = new StreamReader(file.InputStream);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
}
like i said this works if i dont login and its a shot in the dark if you had the same issue i would be more then happy to get a piece of the solution.
The problem was that i could not create the folder while being authenticated, so in this specific case i create folders for users when they register, and using the ftp upload code i show above its working good.