62,074
社区成员
发帖
与我相关
我的任务
分享
default页面,一个html的button和file
然后通过button提交到
<form id="form1" target="UploadWindow" method="post" action="upload.aspx" enctype="multipart/form-data">
upload.aspx页面
default.aspx本页面
function SendUpload()
{
form1.submit();
result = window.setInterval("GetValue()",1000);
}
function GetValue()
{
var percent = "<%=GetUploadStatus() %>";
alert(percent);
document.getElementById("bar").style.width=percent+"%";
if(percent!=100)
alert(percent);
else
window.clearInterval(result);
}
每秒刷新GetUploadStatus()这个方法获取值
default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["UploadInfo"] = new UploadInfo { };
}
}
public int GetUploadStatus()
{
UploadInfo uploadinfo = HttpContext.Current.Session["UploadInfo"] as UploadInfo;
//UploadInfo uploadinfo = new UploadInfo();
if (uploadinfo != null)
{
int soFar = uploadinfo.UploadLength;
int total = uploadinfo.ContentLength;
int percentComplete = (int)Math.Ceiling((double)soFar / (double)total * 100);
//int percentComplete = 5;
string message = string.Format("上传{0}......{1} of {2}", uploadinfo.FileName, uploadinfo.UploadLength, uploadinfo.ContentLength);
return percentComplete;
}
else
return 1000;
}
然后在upload.aspx页面
if (!IsPostBack)
{
UploadInfo uploadinfo = this.Session["UploadInfo"] as UploadInfo;
HttpPostedFile postfile = Request.Files["upload"];
//UploadInfo uploadinfo = new UploadInfo();
uploadinfo.IsReady = true;
if (postfile != null && postfile.ContentLength != 0)
{
string path = Server.MapPath(@"~/upload");
uploadinfo.FileName = Path.GetFileName(postfile.FileName);
uploadinfo.ContentLength = postfile.ContentLength;
uploadinfo.UploadLength = 0;
//uploadinfo.IsReady = true;
int bufferSize = 1;
byte[] buffer = new byte[bufferSize];
using (FileStream fs = new FileStream(Path.Combine(path, uploadinfo.FileName), FileMode.Create))
{
while (uploadinfo.UploadLength < uploadinfo.ContentLength)
{
int bytes = postfile.InputStream.Read(buffer, 0, bufferSize);
fs.Write(buffer, 0, bytes);
uploadinfo.UploadLength += bytes;
Thread.Sleep(100);
}
}
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>window.parent.message('" + uploadinfo.UploadLength + "')</script>");
}
else
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>window.parent.message('请选择文件!')</script>");
uploadinfo.IsReady = false;
}
循环给uploadinfo.UploadLength这个赋值
然后通过这个值除整个图片的大小获取百分比。
可是一直不行,出现了个-2147483648这个值
哪里出问题了呢