在远程服务器上上传 [英] Upload on remote server

查看:76
本文介绍了在远程服务器上上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经阅读了这篇文章,在本地计算机上一切都很好,
但是当我发布此项目并尝试托管它时,代码不起作用,
我必须做什么设置?

1)我必须以编程方式以公共方式创建MSMQ吗?
2)如何在远程服务器上创建c:temp文件?我们无法访问他们的机器?
3)如何在我自己的项目中使用本文?
4)如何创建虚拟页面?该页面将缓存项排队.

请尽快帮助我.

Hi,
i Had go through this article, every this is fine on local machine,
but when i publish this project and trying to host it, code not work,
what settings i have to do?

1)am i have to create MSMQ programmatically as public?
2)How to create c:temp file on remote server? as we cant access their machine?
3)how to use this article in my own project?
4) how to create dummy page?which queues the cache item.

Plz help me as soon as possible.

推荐答案



试试这个:

Hi,

Try this :

/// <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...

Regards,

Algem


这篇关于在远程服务器上上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆