|
聯(lián)系人:李先生 |
電 話:029-87878512 |
手 機(jī):13468700578 |
地 址:西安市絲路國際創(chuàng)意夢(mèng)工廠4號(hào)樓 |
在線咨詢: |
762176615 |
Email:junsoft@126.com |
|
|
|
|
|
|
當(dāng)前的位置 >> 返回首頁 >> 解決方案 |
|
軟件外包中跨服務(wù)器上傳文件的相關(guān)解決方案 |
|
發(fā)布者:西安軟件公司 發(fā)布時(shí)間:2022/8/15 閱讀:次 |
|
第一種:通過FTP來上傳文件
<%@ 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 服務(wù)器地址
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 服務(wù)器上允許上傳的用戶名和密碼
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>將文件上傳到另外一個(gè)服務(wù)器的方法二</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>
第二種:通過WebClient來上傳文件
public class UploadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string filename = context.Request.QueryString["filename"].ToString();
using (FileStream inputStram = File.Create(context.Server.MapPath("UpLoadFiles/") + filename))
{
SaveFile(context.Request.InputStream, inputStram);
}
}
protected void SaveFile(Stream stream, FileStream inputStream)
{
int bufSize=1024;
int byteGet=0;
byte[] buf=new byte[bufSize];
while ((byteGet = stream.Read(buf, 0, bufSize)) > 0)
{
inputStream.Write(buf, 0, byteGet);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
|
|