文件上传ASPWord文档格式.docx
《文件上传ASPWord文档格式.docx》由会员分享,可在线阅读,更多相关《文件上传ASPWord文档格式.docx(14页珍藏版)》请在冰豆网上搜索。
P
MyFile"
<
INPUT
type="
file"
size="
50"
NAME="
File"
/P>
font
color=red>
*
上傳文件不能超過10M<
/font>
P>
asp:
Button
Runat="
Text="
開始上傳"
ID="
UploadButton"
/asp:
Button>
Label
strStatus"
Font-Names="
宋体"
Font-Bold="
True"
Font-Size="
9pt"
Width="
500px"
BorderStyle="
None"
BorderColor="
White"
Label>
/div>
/form>
/body>
/HTML>
-----------------
using
System;
System.Collections;
System.ComponentModel;
System.Data;
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.Data.SqlClient;
System.Configuration;
System.IO;
namespace
WebPortal
{
public
class
Upload
:
System.Web.UI.Page
protected
System.Web.UI.WebControls.Button
UploadButton;
System.Web.UI.WebControls.Label
strStatus;
private
void
Page_Load(object
sender,
System.EventArgs
e)
Session["
uptime"
]=DateTime.Now.Date.ToShortDateString();
uptime2"
]=DateTime.Now.ToString().Replace("
下午"
"
).Replace("
上午"
/"
-"
:
);
if
(this.IsPostBack)
this.SaveImages();
}
Boolean
SaveImages()
HttpFileCollection
files
=
HttpContext.Current.Request.Files;
System.Text.StringBuilder
strMsg
new
System.Text.StringBuilder();
strMsg.Append("
上傳的文件分別是:
hr
try
for(int
iFile
0;
files.Count;
iFile++)
///'
?
查文件?
展名字
HttpPostedFile
postedFile
files[iFile];
string
fileName,
fileExtension,fileSize;
fileName
System.IO.Path.GetFileName(postedFile.FileName);
if((postedFile.ContentLength)>
10485760)
{
Response.Write("
script>
alert('
上傳文件的文件不能超過10M!
'
/script>
}
else
(fileName
!
=
)
fileExtension
System.IO.Path.GetExtension(fileName);
上傳的文件類型:
+
postedFile.ContentType.ToString()
br>
客戶端文件地址:
postedFile.FileName
上傳文件的文件名:
fileName
上傳文件的擴展名:
fileExtension
上傳文件的路?
:
Request.PhysicalApplicationPath
uploadfile"
+"
上?
文件的路?
Server.MapPath(fileName)
上傳文件的路徑:
uploadfile/"
+
+"
上傳文件的大小:
countSize(postedFile.ContentLength)
hr>
可根据?
展名字的不同保存到不同的文件?
///注意:
可能要修改你的文件?
的匿名?
入?
限。
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("
../uploadfile/"
fileName);
//postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("
Session["
]+"
.xls"
nowDSN=ConfigurationSettings.AppSettings["
data"
];
SqlConnection
myConnection=new
SqlConnection(nowDSN);
CommandText;
SqlCommand
myCommand
SqlCommand();
myCommand.Connection=myConnection;
myCommand.Connection.Open();
filename3=Session["
;
myCommand.CommandText=
insert
into
uploadfile(filename,filesize,filemappath,fileExtension,uptime,filename2)values('
+fileName+"
'
+countSize(postedFile.ContentLength)+"
+Request.PhysicalApplicationPath+"
uploadfile\\"
+fileExtension+"
+Session["
+filename3+"
)"
myCommand.ExecuteNonQuery();
上傳文件的文件不能為空!
strStatus.Text
strMsg.ToString();
return
true;
catch(System.Exception
Ex)
Ex.Message;
false;
#region
Web
窗体?
器生成的代?
override
protected
OnInit(EventArgs
InitializeComponent();
base.OnInit(e);
InitializeComponent()
this.ID
Upload"
this.Load
+=
System.EventHandler(this.Page_Load);
string
countSize(long
size)
//?
是?
算文件大小的方法
strSize="
long
lnSize=0;
lnSize=size;
if(lnSize<
1024)
strSize=lnSize.ToString()+"
byte"
if(lnSize>
=1024
&
&
lnSize<
1048576)
strSize=(lnSize/1024).ToString()
K"
=1048576
=10485760)
strSize=(lnSize/1024/1024).ToString()
M"
strSize;
#endregion
}
哦,差点看错了,是C#编程的呀
可以看看这个.
用C#写的文件断点续传、下载的类
本用例是采用C#上传、Servlet进行接收
C#作为客户端、Servlet作为服务器进行文件断点下载
有调试不通过的,给我Mail:
lijiangchxp@
usingSystem;
usinglog4net;
usingSystem.Collections;
usingSystem.Text;
usingSystem.IO;
usingSystem.Net;
usinglog4net.Config;
usingChxp.Business;
namespaceChxp.Service
{
publicclassFileLib
{
#region属性
privatestringfileName="
"
publicstringFileName
get{returnfileName;
}
set{fileName=value;
#endregion
privatestaticreadonlyILogLOG=LogManager.GetLogger(typeof(FileLib));
#region文件上传
///<
summary>
///上传文件(自动分割)
/summary>
paramname="
filePath"
待上传的文件全路径名称(@"
E:
\FTP\ftproot\20070228DQCK.zip"
)<
/param>
hostURL"
服务器的地址<
byteCount"
分割的字节大小<
userID"
主机用户ID<
cruuent"
当前字节指针<
returns>
成功返回"
失败则返回错误信息<
/returns>
publicstringUpLoadFile(stringfilePath,stringhostURL,intbyteCount,stringuserID,longcruuent)
stringtmpURL=hostURL;
byteCount=byteCount*1024;
//http:
//localhost:
8080/fism/app?
service=fileupload&
beanId=com.cfcc.fism.service.upload.CollFileSaveServiceImpl&
action=upload&
filename=AI1215900000020051130411.zip&
userid=test&
npos=333
//action=length
System.Net.WebClientWebClientObj=newSystem.Net.WebClient();
FileStreamfStream=newFileStream(filePath,FileMode.Open,FileAccess.Read);
BinaryReaderbReader=newBinaryReader(fStream);
longlength=fStream.Length;
stringsMsg="
版式上传成功"
stringfileName=filePath.Substring(filePath.LastIndexOf('
\\'
)+1);
try
#region续传处理
byte[]data;
if(cruuent>
0)
fStream.Seek(cruuent,SeekOrigin.Current);
#region分割文件上传
for(;
cruuent<
=length;
cruuent=cruuent+byteCount)
{
if(cruuent+byteCount>
length)
data=newbyte[Convert.ToInt64((length-cruuent))];
bReader.Read(data,0,Convert.ToInt32((length-cruuent)));
else
data=newbyte[byteCount];
bReader.Read(data,0,byteCount);
LOG.Debug(data);
//***
hostURL=tmpURL+"
action=upload"
filename="
+fileName+"
userid="
+userID+"
npos="
+cruuent.ToString();
byte[]byRemoteInfo=WebClientObj.UploadData(hostURL,"
POST"
data);
stringsRemoteInfo=System.Text.Encoding.Default.GetString(byRemoteInfo);
//获取返回信息
if(sRemoteInfo.Trim()!
="
)
sMsg=sRemoteInfo;
break;
catch(Exceptionex)
sMsg=ex.ToString();
#endregion
sMsg=sMsg+ex.ToString();
bReader.Close();
fStream.Close();
catch(ExceptionexMsg)
sMsg=exMsg.ToString();
GC.Collect();
returnsMsg;
}
#region获取文件大小
///获取远程服务器文件字节大小
待上传的文件全路径名称<
远程文件大小<
publiclongGetRemoteFileLength(stringfilePath,stringhostURL,stringuserID)
longlength=0;
hostURL=hostURL+"
action=length"
npos=0"
;
byte[]data=newbyte[0];
byte[]byRemoteInfo=WebClientObj.UploadData(hostURL,"
//主系统没有作异常处理
length=Convert.ToInt64(sRemoteInfo);
catch(Exceptionexx)
LOG.Error("
FileLib类GetRemoteFileLength()中length=Convert.ToInt64(sRemoteInfo)语句异常:
+exx.Message);
//我们强制处理异常
length=0;
returnlength;
///获得本地文件字节大小
本地文件全路径<
本地文件字节大小<
publiclongGetLocalFileLength(stringfilePath)
FileStreams=newFileStream(filePath,FileMode.Open);
length=s.Length;
s.Close();
catch(Exceptionex)
FileLib类中获取本地文件大小异常:
+ex.Message);
#region文件下载
publicboolDownLoadFile(stringlocalPath,stringhostURL,intbyteCount,stringuserID,longcruuent)
boolresult=true;
System.IO.FileStr