將檔案上傳到網際網路共用伺服器的方法

來源:互聯網
上載者:User

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
&
gt;
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
"
);
            }

        }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.