|
|
|
当前的位置 >> 返回首页 >> 解决方案 |
|
软件外包中跨服务器上传文件的相关解决方案 |
|
发布者:西安软件公司 发布时间: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 服务器地址
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>
第二种:通过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;
}
}
}
|
|