ASP.NET 中将文件上传到另外一个服务器
来源:互联网 发布:爱思mac版下载 编辑:程序博客网 时间:2024/05/21 09:48
http://bbs.csdn.net/topics/390210927
if
(Ping(
"192.168.0.144"
))
//Ping 看通不通
{
if
(Connect(
"192.168.0.144"
,
"administrator"
,
"1qaz2wsx???"
))
//建立连接
{
_fileStream =
new
FileStream(
@"\\192.168.0.144\hh\"
+ _currentFileName, FileMode.OpenOrCreate);
}
}
#region 连接远程机器
public
static
bool
Ping(
string
remoteHost)
{
bool
Flag =
false
;
Process proc =
new
Process();
try
{
proc.StartInfo.FileName =
"cmd.exe"
;
proc.StartInfo.UseShellExecute =
false
;
proc.StartInfo.RedirectStandardInput =
true
;
proc.StartInfo.RedirectStandardOutput =
true
;
proc.StartInfo.RedirectStandardError =
true
;
proc.StartInfo.CreateNoWindow =
true
;
proc.Start();
string
dosLine =
@"ping -n 1 "
+ remoteHost;
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine(
"exit"
);
while
(proc.HasExited ==
false
)
{
proc.WaitForExit(500);
}
string
pingResult = proc.StandardOutput.ReadToEnd();
if
(pingResult.IndexOf(
"(0% loss)"
) != -1)
{
Flag =
true
;
}
proc.StandardOutput.Close();
}
catch
(Exception ex)
{
}
finally
{
try
{
proc.Close();
proc.Dispose();
}
catch
{
}
}
return
Flag;
}
public
static
bool
Connect(
string
remoteHost,
string
userName,
string
passWord)
{
if
(!Ping(remoteHost))
{
return
false
;
}
bool
Flag =
true
;
Process proc =
new
Process();
try
{
proc.StartInfo.FileName =
"cmd.exe"
;
proc.StartInfo.UseShellExecute =
false
;
proc.StartInfo.RedirectStandardInput =
true
;
proc.StartInfo.RedirectStandardOutput =
true
;
proc.StartInfo.RedirectStandardError =
true
;
proc.StartInfo.CreateNoWindow =
true
;
proc.Start();
string
dosLine =
@"net use \\"
+ remoteHost +
" "
+ passWord +
" "
+
" /user:"
+ userName +
">NUL"
;
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine(
"exit"
);
while
(proc.HasExited ==
false
)
{
proc.WaitForExit(1000);
}
string
errormsg = proc.StandardError.ReadToEnd();
if
(errormsg !=
""
)
{
Flag =
false
;
}
proc.StandardError.Close();
}
catch
(Exception ex)
{
Flag =
false
;
}
finally
{
try
{
proc.Close();
proc.Dispose();
}
catch
{
}
}
return
Flag;
}
#endregion
排版差了点 请谅解。 测试通过
- sibiyellow
- sibiyellow
- 等级:
- 结帖率:95.88%
将文件上传到网络共享服务器的方法
1,在文件服务器上,创建一个本地帐户,比如登录名:upload,密码:upload,注意在创建的时候选择“密码永不过期”,去掉勾选“用户下次登录时须更改密码”的选项;
2,在要共享的文件夹上点右键,选择“属性”-“安全”,增加upload帐户可以写入的权限;
3,在要共享的文件夹上点右键,选择“共享”,共享此文件夹,并在“权限”按钮点击后添加帐户upload可修改;
4,在另外一台 Web 服务器上,创建登录名和密码与上面完全相同的本地帐户。
5,在web.config里,启用模拟:
web.config里添加的代码
<identity impersonate="true" userName="upload" password="upload" />
6,在网站文件夹和Temporary ASP.NET Files文件夹上设置帐户upload读写权限
7,在ASP.NET的上传文件里写:
C# 代码
protected void Button1_Click(object sender, EventArgs e)
{
string fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(@"\\192.168.3.1\free\" + fileName);
}
8,显示上传的文件:
在IIS里创建虚拟目录,指向“另一台计算机上的共享”,“连接为”输入上面创建的帐户名和密码。即可以http://www.mengxianhui.com/upload/hello.jpg进行访问。
注意:在VS里面直接运行可能会报告
Could not load file or assembly 'WebApplication1' or one of its dependencies. 拒绝访问。
这是因为你模拟的帐户没有权限导致的,你可以发布到IIS看效果。
下面是一段使用程序进行模拟的方法,出自 http://2leggedspider.wordpress.com/2007/05/28/upload-files-to-unc-share-using-asp-net/ :
C# 代码
using
System.Security.Principal;
using
System.Runtime.InteropServices;
namespace
FileUploadUNCShare
{
public
partial
class
_Default : System.Web.UI.Page
{
public
const
int
LOGON32_LOGON_INTERACTIVE = 2;
public
const
int
LOGON32_PROVIDER_DEFAULT = 0;
WindowsImpersonationContext impersonationContext;
[DllImport(
"advapi32.dll"
)]
public
static
extern
int
LogonUserA(String lpszUserName,
String lpszDomain,
String lpszPassword,
int
dwLogonType,
int
dwLogonProvider,
ref
IntPtr phToken);
[DllImport(
"advapi32.dll"
, CharSet = CharSet.Auto, SetLastError =
true
)]
public
static
extern
int
DuplicateToken(IntPtr hToken,
int
impersonationLevel,
ref
IntPtr hNewToken);
[DllImport(
"advapi32.dll"
, CharSet = CharSet.Auto, SetLastError =
true
)]
public
static
extern
bool
RevertToSelf();
[DllImport(
"kernel32.dll"
, CharSet = CharSet.Auto)]
public
static
extern
bool
CloseHandle(IntPtr handle);
private
bool
ImpersonateUser(String userName, String domain, String password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if
(RevertToSelf())
{
if
(LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,
ref
token) != 0)
{
if
(DuplicateToken(token, 2,
ref
tokenDuplicate) != 0)
{
tempWindowsIdentity =
new
WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if
(impersonationContext !=
null
)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return
true
;
}
}
}
}
if
(token != IntPtr.Zero)
CloseHandle(token);
if
(tokenDuplicate != IntPtr.Zero)
CloseHandle(tokenDuplicate);
return
false
;
}
private
void
UndoImpersonation()
{
impersonationContext.Undo();
}
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
if
(ImpersonateUser(
"upload"
,
""
,
"upload"
))
{
if
((FileUpload1.PostedFile !=
null
) && (FileUpload1.PostedFile.ContentLength > 0))
{
string
fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
string
folderPath =
@"\\MyUNCShare\MyFolder\"
;
string
locationToSave = folderPath +
"\\"
+ fileName;
try
{
FileUpload1.PostedFile.SaveAs(locationToSave);
Response.Write(
"The file has been uploaded."
);
}
catch
(Exception ex)
{
Response.Write(
"Error: "
+ ex.Message);
}
}
else
{
Response.Write(
"Please select a file to upload."
);
}
UndoImpersonation();
}
else
{
Response.Write(
"Failed"
);
}
}
}
}
- ASP.NET 中将文件上传到另外一个服务器
- ASP.NET 中将文件上传到另外一个服务器的方法二
- ASP.NET 中将文件上传到另外一个服务器的方法
- ASP.NET 中将文件上传到另外一个服务器的方法二
- ASP.Net如何把文件从一台服务器上传到另外一台服务器
- asp.net如何把文件上传到另外一台服务器[CSDN]。
- asp.net如何把文件上传到另外一台服务器
- ASP.NET实现上传文件到服务器
- ASP.NET 上传文件到服务器
- ASP.NET中上传大文件到服务器的解决方案
- ASP.NET基础教程-从客户端上传文件到服务器
- 使用ASP.NET上传多个文件到服务器
- 使用ASP.NET上传多个文件到服务器
- asp.net如何上传或下载文件到远程服务器
- 使用ASP.NET上传多个文件到服务器
- ASP.NET中上传大文件到服务器的解决方案
- ASP.NET2.0中将文件上传到数据库
- ASP.NET2.0中将文件上传到数据库
- linux/list.h (
- 百度搜索结果页面的参数 输入编码(ie)
- C++构造函数之深拷贝与浅拷贝
- linux/err.h
- window.name实现的跨域数据传输
- ASP.NET 中将文件上传到另外一个服务器
- CATransform3D-Cube
- 一个不错的正则表达式分析工具
- EntityManager方法简介
- ATA Disk在Linux中的驱动架构对比分析
- Linux2.6.28中Scsi Disk驱动IO回调路径分析
- android控件之TextView
- JavaSE基础:正则表达式
- vs2010中臃肿的ipch和sdf文件