Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I'm using vs2008. I need to upload a file to a remote server.there is a folder called 'Files' on my site and I'm supposed to move all uploaded files there.

Currently, when I use the
VB
myFile.PostedFile.SaveAs()
method, all works well locally. But when I upload to my remote server, there goes the issue.

I have given the folder appropriate permissions, but myFile.PostedFile.SaveAs() seems to always see my local directory.

Here it is:

VB
Dim dirPath As String = AppDomain.CurrentDomain.BaseDirectory + "Files"
myFile.PostedFile.SaveAs(dirPath & "/" & strFileNameOnly).


Note: myFileis the id of the upload file control

Any help appreciated
Posted

Hi,

I think you should pass a credential to validate
eg.: uploadRequest.Credentials = new NetworkCredential(user, pswd);

I have written a code in c sharp. I leave it to you to translate it:

See below code:

/// <summary>
     /// Author: Algem mojedo
     /// Date: Aug. 12, 2011
     /// </summary>
     /// <param name="domainName"></param>
     /// <param name="targetRemoteUploadUrl"></param>
     /// <param name="fileName"></param>
     /// <param name="user"></param>
     /// <param name="pswd"></param>
     /// <returns></returns>
     public string Upload(string domainName, string targetRemoteUploadUrl, string fileName, string user, string pswd)
     {
         string result = string.Empty;
         Stream requestStream = null;
         FileStream fileStream = null;
         System.Net.FileWebResponse uploadResponse = null;
         try
         {
             string uriFile = HyperLinkToUrl(targetRemoteUploadUrl) + fileName.Substring((fileName.LastIndexOf("\\")));
             Uri processFile = new Uri(uriFile);

             System.Net.FileWebRequest uploadRequest = (System.Net.FileWebRequest)WebRequest.Create(processFile);

             if (!System.IO.Directory.Exists(targetRemoteUploadUrl))
             {
                  return "Error: " + "Target server/folder not exist...";
             }
             uploadRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;

             //Since the FTP you are downloading to is secure, send
             //in user name and password to be able upload the file
             if (user != string.Empty && pswd == string.Empty)
             {
                    return "Error: " + "Password should not empty...";
             }
             if (user == string.Empty && pswd != string.Empty)
             {
                  return "Error: " + "User ID should not empty...";
             }

             if (user != string.Empty && pswd != string.Empty)
             {
                 uploadRequest.Credentials = new NetworkCredential(user, pswd);
                 bool valid = false;
                 PrincipalContext context = new PrincipalContext((ContextType.Domain), domainName);
                 valid = context.ValidateCredentials(user, pswd);
                 if (!valid)
                 {
                     return "Error: " + "Invalid User Id and/or Password...";
                 }
             }
             //UploadFile is not supported through an Http proxy
             //so we disable the proxy for this request.
             uploadRequest.Proxy = null;
             requestStream = uploadRequest.GetRequestStream();
             fileStream = File.Open(fileName, FileMode.Open);
             byte[] buffer = new byte[1024];
             int bytesRead;
             while (true)
             {
                 bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                 if (bytesRead == 0)
                     break;
                 requestStream.Write(buffer, 0, bytesRead);
             }
             //The request stream must be closed before getting
             //the response.
             requestStream.Close();
             uploadResponse = (System.Net.FileWebResponse)uploadRequest.GetResponse();
         }
         catch (UriFormatException ex)
         {
             return "Error: " + ex.Message;
         }
         catch (IOException ex)
         {
             return "Error: " + ex.Message;
         }
         catch (WebException ex)
         {
             return "Error: " + ex.Message;
         }
         finally
         {
             if (uploadResponse != null)
                 uploadResponse.Close();
             if (fileStream != null)
                 fileStream.Close();
             if (requestStream != null)
                 requestStream.Close();

         }
         return string.Empty;
     }

     // Aug. 12, 2011 by Algem Mojedo
     // clalled from Upload
     public string HyperLinkToUrl(string save2File)
     {
         save2File = save2File.ToLower();
         if (!string.IsNullOrEmpty(save2File))
         {
             var reg = new Regex(@"(?                return reg.Replace(save2File, new MatchEvaluator(ConvertUrlsMatchDelegate));
         }
         return save2File;
     }



Hope this could help...

Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Regards,

Algem
 
Share this answer
 
v2
Comments
Dalek Dave 8-Sep-11 3:33am    
Good Answer.
I've tried converting all but I get this error at the underlined.
VB
Dim context As PrincipalContext = New PrincipalContext((ContextType.DomainUnknown), domainName)
                valid = Context.ValidateCredentials(user, pswd)


PrincipalContext not defined
 
Share this answer
 
Comments
Al Moje 8-Sep-11 3:42am    
I think you must supply the domain name of the remote site.
example: 'CONTOSO_DC' this should be the exact domain name of your target remote ftp.
and also the exact password to access such Domain.
infinitizon 8-Sep-11 10:14am    
Ok, I get you. But I really need to get something clear.

My remote server is a computer within the company (an intranet). So, I normally dont need to use ftp when I want to access it (I just do something like - 192.168.1.177 - on the address bar). So then, do you have ideas how to set ftp account on a system so that I can use your method
Al Moje 9-Sep-11 2:30am    
Hi,
You may pass as like this example:
"\\192.168.1.177\ClientData\AlgemTest". The '\ClientData\AlgemTest' is my sub directory target.

Note: Also that the value in
UrlToHyperLink(string save2File) function was change when I post above. This should be:

var reg = new Regex(@"(?<!<\s*(?:a|img)\b[^<]*)(\b((http|https)://|www\.)[^ ]+\b)");
return reg.Replace(save2File, new MatchEvaluator(ConvertUrlsMatchDelegate));
Al Moje 9-Sep-11 2:36am    
Hi,
This is it:
// Aug. 12, 2011 by Algem Mojedo
// clalled from Upload
public string HyperLinkToUrl(string save2File)
{
save2File = save2File.ToLower();
if (!string.IsNullOrEmpty(save2File))
{
var reg = new Regex(@"(?<!<\s*(?:a|img)\b[^<]*)(\b((:\\|https)://|www\.)[^ ]+\b)");
return reg.Replace(save2File, new MatchEvaluator(ConvertUrlsMatchDelegate));
}
return save2File;
}

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