文件上传到服务器
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
using System;
usingSystem.Collections;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
using System.IO;
public partial class 上传文件代码: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btu_Click(object sender, EventArgs e)
{
//上传图片分为两步第一步:将图片上传到服务器第二步:将路径保存到数据库
string path = ""; //创建路径
string filenewname = DateTime.Now.ToLongDateString() + DateTime.Now.Hour.ToString() + lisecond.ToString(); //文件名
if (this.fileupload.PostedFile.FileName.Length> 0)
{
intipos = stIndexOf("\\"); //记录最后一个\ 的位置
string filename = "";
if (ipos.ToString() != "-1")
{
filename =
this.fileupload.PostedFile.FileName.Substring(ipos);//获取文件名
}
else
{
filename = this.fileupload.PostedFile.FileName;
}
intiiii = stIndexOf(".");
string fileexpandname = filename.Substring(iiii); //获取扩展名fileexpandname = fileexpandname.ToLower();
if (fileexpandname == ".jpg" || fileexpandname == ".doc" || fileexpandname == ".gif")
{
string filefolder = "\\uploadfile\\news"; //构造文件夹名称
path = filefolder + "\\" + filenewname + fileexpandname;
DirectoryInfo dire = new DirectoryInfo(Server.MapPath(filefolder)); //创建文件夹(在本机上用就去掉Server.MapPath)
if (!dire.Exists)
{
dire.Create();
}
DirectoryInfodires = new DirectoryInfo(Server.MapPath(filefolder + "\\s_img")); //创建文件夹(在本机上用就去掉Server.MapPath)
if (!dires.Exists)
{
dires.Create();
}
}
this.fileupload.PostedFile.SaveAs(Server.MapPath(path)); //100*150(在本机上用就去掉Server.MapPath)
}
else
{
Response.Write("");
}
}
}
222222222222222222222222222222222222222222222222
.net字节流上传文件
///
/// WebClient上传文件至服务器(不带进度条)
///
/// 要上传的文件(全路径格式)
/// Web服务器文件夹路径
///
public boolUpLoadFile(string fileNameFullPath, string strUrlDirPath)
{
//得到要上传的文件文件名
string fileName = fileNameFullPath.Substring(stIndexOf("\\") + 1);
//新文件名由年月日时分秒及毫秒组成
string NewFileName = DateTime.Now.ToString("yyyyMMddhhmmss")
+ lisecond.ToString()
+ fileNameFullPath.Substring(stIndexOf("."));
//得到文件扩展名
string fileNameExt = fileName.Substring(stIndexOf(".") + 1);
if (strUrlDirPath.EndsWith("/") == false) strUrlDirPath = strUrlDirPath + "/";
if (Directory.Exists(Server.MapPath(strUrlDirPath)))
{
Directory.CreateDirectory(Server.MapPath(strUrlDirPath));
}
//保存在服务器上时,将文件改名(示业务需要)
strUrlDirPath = strUrlDirPath + NewFileName;
// 创建WebClient实例
WebClientmyWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
// 将要上传的文件打开读进文件流
FileStreammyFileStream = new FileStream(fileNameFullPath, FileMode.Open, FileAccess.Read);
BinaryReadermyBinaryReader = new BinaryReader(myFileStream);
try
{
byte[] postArray = myBinaryReader.ReadBytes((int)myFileStream.Length);
//打开远程Web地址,将文件流写入
Stream postStream = myWebClient.OpenWrite(strUrlDirPath, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
else
{
//MessageBox.Show("Web服务器文件目前不可写入,请检查Web服务器目录权限设置!","系统提示",MessageBoxButtons.OK,rmation);