如何将上传的文件移动到远程服务器上的文件夹 [英] How to move uploaded file to a folder on remote server
问题描述
我正在使用vs2008.我需要将文件上传到远程服务器.我的站点上有一个名为文件"的文件夹,我应该将所有上传的文件移到那里.
当前,当我使用
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
myFile.PostedFile.SaveAs()
方法时,所有方法在本地都可以正常工作.但是当我上传到远程服务器时,就会出现问题.
我已授予该文件夹适当的权限,但是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:
Dim dirPath As String = AppDomain.CurrentDomain.BaseDirectory + "Files"
myFile.PostedFile.SaveAs(dirPath & "/" & strFileNameOnly).
注意:myFile是上传文件控件的ID.
感谢任何帮助
Note: myFileis the id of the upload file control
Any help appreciated
推荐答案
我认为您应该通过证书来验证
例如: uploadRequest.Credentials = new NetworkCredential(user,pswd);
我已经用C Sharp编写了代码.我留给你翻译:
参见以下代码:
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;
}
希望这可以帮助...
请记住,如果有帮助,请将其标记为答案;如果没有帮助,则将其取消标记.
问候,
Algem
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
我已经尝试了全部转换,但在带下划线的地方出现了此错误.
I''ve tried converting all but I get this error at the underlined.
Dim context As PrincipalContext = New PrincipalContext((ContextType.DomainUnknown), domainName)
valid = Context.ValidateCredentials(user, pswd)
PrincipalContext未定义
PrincipalContext not defined
这篇关于如何将上传的文件移动到远程服务器上的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!