我想让数据库数据和文件数据分离,数据库数据插入到本机,文件上传到另外一个服务器上。这要怎么处理?
解决方案 »
- sql更新语句的问题
- 那里有ajax的例子,适合学习的那种,不要太难!一直做项目没有给学习!顺便散分
- 关于DataGrid控件中的数据编辑的问题,请帮忙看一下。
- IIS与Oracle9i共存的问题??急救,请指教!!
- 怎么实现最近搜索过的条件
- 急啊!各位大哥帮帮忙啊!关于水晶报表登陆失败的问题
- 请问如何在程序中修改DATAGRID列的信息?
- 请问怎么控制HTML输出?
- 请问:SqlDataAdapter在自动Update的时候,时候可以处理数据库中的二进制数据?
- 请问如何使用SqlDataAdapter 的update方法更新数据库?
- c#正则问题!为什么匹配不出下列网址?
- 关于正则提取网址的问题,在线等~十万火急
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 中将文件上传到另外一个服务器的方法二前面的文章,我提供了一种”将文件上传到网络共享服务器的方法“,这次,我们采用FTP的方法将文件上传到另外一台服务器上。
首先,在另外一台服务器上设置好FTP服务,并创建好允许上传的用户和密码,然后,在ASP.NET里就可以直接将文件上传到这台 FTP 服务器上了。代码如下:
ASPX 代码
<%@ Page Language="C#" EnableViewState="false"%><%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
//要接收文件的 ftp 服务器地址
String serverUri = "ftp://192.168.3.1/";
String fileName = Path.GetFileName(FileUpload1.FileName);
serverUri += fileName;
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.Method = WebRequestMethods.Ftp.AppendFile;
request.UseBinary = true;
request.UsePassive = true;
// ftp 服务器上允许上传的用户名和密码
request.Credentials = new NetworkCredential("upload", "upload");
Stream requestStream = request.GetRequestStream();
Byte[] buffer = FileUpload1.FileBytes; requestStream.Write(buffer, 0, buffer.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Label1.Text = response.StatusDescription;
response.Close();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>将文件上传到另外一个服务器的方法二</title>
</head>
<body>
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传文件" />
<div><asp:Label ID="Label1" runat="server" Text=""></asp:Label></div>
</form>
</body>
</html>