Click here to Skip to main content
15,900,464 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
protected void Button2_Click(object sender, EventArgs e)
        {

            if (MyFile.PostedFile != null)
            {
                HttpPostedFile uploadFile = MyFile.PostedFile;
                if (System.IO.Path.GetExtension(uploadFile.FileName).ToLower() == ".jpg")
                {
                    string strFileName = Path.GetFileName(uploadFile.FileName);


                    FileInfo fileInf = new FileInfo(strFileName);
                   
                    FtpWebRequest reqFTP;

                
                   reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://111.111.113./httpdocs/uploadpic/" + fileInf.Name));
                   reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                   reqFTP.Credentials = new NetworkCredential("music", "123");

                    reqFTP.KeepAlive = true;

                    reqFTP.UseBinary = true;
                
                    Stream ftpStream = reqFTP.GetRequestStream();
                    FileStream file = fileInf.OpenRead();

                    int length = 1024;
                    byte[] buffer = new byte[length];
                    int bytesread = 0;

                   do
                   {
                      bytesread = file.Read(buffer, 0, length);
                        ftpStream.Write(buffer, 0, bytesread);
                   }
                     while (bytesread != 0);

                  file.Close();
                 ftpStream.Close();
       
                }
            }

error msg
Could not find file 'C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\IDE\IMG_6391.JPG'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IO.FileNotFoundException: Could not find file 'C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\IDE\IMG_6391.JPG'.

Source Error: 


Line 108:                
Line 109:                    Stream ftpStream = reqFTP.GetRequestStream();
Line 110:                **    FileStream file = fileInf.OpenRead();
Line 111:
Line 112:
Posted
v2
Comments
Sergey Alexandrovich Kryukov 13-Mar-13 1:59am    
Pathway... file path? On server side or client side?
—SA
kangyi.lee 13-Mar-13 2:09am    
pathway from client side
Sergey Alexandrovich Kryukov 13-Mar-13 2:26am    
file path...
kangyi.lee 13-Mar-13 2:34am    
randomly choose one, no fixed file path
Sergey Alexandrovich Kryukov 13-Mar-13 2:36am    
It does not really matter. I answered in detail, please see.
—SA

1 solution

Thank you for clarification.

You cannot get this information. Client-side full path information is not provided by a browser for uploading, for important security reasons. You never really need it: a user provides the file itself, it does not matter where this file was taken; the user know that; and this is all that matters.

Any technique relying on this information is wrong. Just think about it: when the same user uploads some file again, she/he can use a completely different computer. If you store the previous path somewhere on the server, how can it help? The client side is a different story: browsers usually store the path information from previous downloads, for user's convenience. It does not compromise security because this information is not exposed to outside world.

And the most important aspect here is security. The server should not "know" any detail on the client system.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900