开发者

error uploading files while logged in

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.


When you log in, IIS is trying to impersonate you and you do not have access to that folder, only the IIS user does.
Several options to debug your code. My preference on a remote server is to add Response.write before every critical location in the file, for example after System.Web.HttpContext.Current.Server.MapPath("~/" + path + "/"); which will tell you if you got to that step and potentially (if you set it) information about the request such as the current security context/user.

Example:

foreach (string item in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[item];
                if (file != null)
                {
Response.Write("A");
                    var path = "File";
                    var filePath = System.Web.HttpContext.Current.Server.MapPath("~/" + path + "/");
    Response.Write("B");
                    if (!Directory.Exists(filePath))
                    {
                        Response.Write("C");
                        Directory.CreateDirectory(filePath);
                    }
Response.Write("D");
                    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();

                }

If you start with that, and you see the output ABCD, then you know the problem is further down, so just repeat the process. (I find it overly distracting to add too many debugging lines at once, but you could.)
If you stop at, say "ABC", then you know that Directory.CreateDirectory(filePath); failed. You can try to do Response.Write(HttpContext.Current.User.Identity) when you are logged in and not logged in and see what you get. (I am not sure that is the property you want, but I can't test it right now, so you can try to look it up or just experiment.)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜