FileUpload上传文件时我想在改变或浏览后,用image显示图片,怎么做?下面是我的代码:
<headrunat="server"><title>FileUpload</title><scripttype="text/javascript"language="j...
<head runat="server">
<title>FileUpload</title>
<script type="text/javascript" language="javascript">
function test(a) {
{
if(document.getElementById("FileUpload1").value!="")
{
document.all.itmem("Image1").src=document.getElementById("FileUpload1").value;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" onchange="test(this)" /><br />
<asp:Image ID="Image1" runat="server" /><br />
<asp:Button ID="button1" runat="server" Text="上传图片" onclick="button1_Click" /><br />
<asp:Label ID="label1" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html> 展开
<title>FileUpload</title>
<script type="text/javascript" language="javascript">
function test(a) {
{
if(document.getElementById("FileUpload1").value!="")
{
document.all.itmem("Image1").src=document.getElementById("FileUpload1").value;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" onchange="test(this)" /><br />
<asp:Image ID="Image1" runat="server" /><br />
<asp:Button ID="button1" runat="server" Text="上传图片" onclick="button1_Click" /><br />
<asp:Label ID="label1" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html> 展开
展开全部
public static Boolean FileUpload(HttpPostedFile postFile)
{
HttpPostedFile file =postFile;//调用标准类库,方法
int contentLength = file.ContentLength;//文件长度
string fileType = Path.GetExtension(file.FileName);
if (string.Compare(fileType,".rar",true)!=0)//判断上传文件的类型
return false;
if (contentLength > 10240000)//上传文件必须小于10M
return false;
try
{
string fileFullName = Path.GetFileName(file.FileName);//带扩展名的文件名
string fileName = Path.GetFileNameWithoutExtension(file.FileName);//同上,不带扩展名
HttpRequest request = System.Web.HttpContext.Current.Request;
string uploadPath =request.MapPath("..\\Download\\DownloadFile\\" + fileFullName);//上传路径
FileStream fs = new FileStream(uploadPath, FileMode.Create);//如果存在则覆盖原来文件
BinaryWriter bw = new BinaryWriter(fs);
byte[] buffer = new Byte[10000];//确定数据包大小
while (contentLength > 0)
{
int length = file.InputStream.Read(buffer, 0, 10000);
bw.Write(buffer, 0, length);
buffer = new Byte[10000];
contentLength -= length;
}
fs.Close();
bw.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
{
HttpPostedFile file =postFile;//调用标准类库,方法
int contentLength = file.ContentLength;//文件长度
string fileType = Path.GetExtension(file.FileName);
if (string.Compare(fileType,".rar",true)!=0)//判断上传文件的类型
return false;
if (contentLength > 10240000)//上传文件必须小于10M
return false;
try
{
string fileFullName = Path.GetFileName(file.FileName);//带扩展名的文件名
string fileName = Path.GetFileNameWithoutExtension(file.FileName);//同上,不带扩展名
HttpRequest request = System.Web.HttpContext.Current.Request;
string uploadPath =request.MapPath("..\\Download\\DownloadFile\\" + fileFullName);//上传路径
FileStream fs = new FileStream(uploadPath, FileMode.Create);//如果存在则覆盖原来文件
BinaryWriter bw = new BinaryWriter(fs);
byte[] buffer = new Byte[10000];//确定数据包大小
while (contentLength > 0)
{
int length = file.InputStream.Read(buffer, 0, 10000);
bw.Write(buffer, 0, length);
buffer = new Byte[10000];
contentLength -= length;
}
fs.Close();
bw.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询