62,046
社区成员
发帖
与我相关
我的任务
分享
protected void btnUploadImg_Click(object sender, EventArgs e)
{
string RootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath);
string TempImgPath = RootDir + "\\images\\TempImg.jpg";
if (FileUpload1.FileName != null)
{
CreateTempImg(ImgToBytes(FileUpload1.PostedFile.FileName), TempImgPath);
Image1.ImageUrl = urlconvertor(TempImgPath);
}
}
/// <summary>
///将参数imagesurl1的绝对路径转换为相对路径
/// </summary>
/// <param name="imagesurl1">当前的绝对路径</param>
/// <returns>返回当前项目的相对路径</returns>
public string urlconvertor(string imagesurl1)
{
string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
string imagesurl2 = "~" + imagesurl1.Replace(tmpRootDir, ""); //转换成相对路径
imagesurl2 = imagesurl2.Replace(@"\", @"/");
return imagesurl2;
}
/// <summary>
///以img为源图片,在指定的路径path下复制创建一个临时图片
/// </summary>
/// <param name="img">当前源图片的二进制流</param>
/// <param name="path">临时图片的储存路径</param>
protected void CreateTempImg(byte[] img,string path)
{
FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(img);
//=====================注意此处,一定要将文件流关闭,否则临时文件一直会处于使用状态,页面将无法读取
bw.Close();
fs.Dispose();
}
/// <summary>
///将图片转换为对应的二进制流
/// </summary>
/// <param name="path">源图片的储存路径</param>
/// <returns>返回当前图片的二进制流</returns>
protected byte[] ImgToBytes(string path)
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read );
BinaryReader br = new BinaryReader(fs);
return br.ReadBytes((int)fs.Length);
}
<div id="preview" style="width:860px;height:120px">
<img alt="" id="imghead" runat="server" style="height:120px"/>
</div>
<br />
<input type="file" runat="server" id="fileup1" onchange="previewImage(this)" />
[/quote]
这个我已经看到了。谢谢!
但是有没有人知道我提出的另外两个问题呢?
1、FileUpload1.PostedFile.FileName的值在没有调整IE浏览器的安全级别之前,一直返回的是文件名,而不是全路径,这个除了调低IE的安全级别,还有其他的方法么?
2、image控件的ImageUrl属性只能支持相对地址么?好像用绝对地址就不能加载图片了。
<div id="preview" style="width:860px;height:120px">
<img alt="" id="imghead" runat="server" style="height:120px"/>
</div>
<br />
<input type="file" runat="server" id="fileup1" onchange="previewImage(this)" />
<script type="text/javascript">
function previewImage(file) {
var MAXWIDTH = 860;
var MAXHEIGHT = 120;
var div = document.getElementById('preview');
if (file.files && file.files[0]) {
div.innerHTML = '<img id=imghead>';
var img = document.getElementById('imghead');
img.onload = function () {
var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
img.width = rect.width;
img.height = rect.height;
img.style.marginLeft = rect.left + 'px';
img.style.marginTop = rect.top + 'px';
}
var reader = new FileReader();
reader.onload = function (evt) { img.src = evt.target.result; }
reader.readAsDataURL(file.files[0]);
}
else {
var sFilter = 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
file.select();
var src = document.selection.createRange().text;
div.innerHTML = '<img id=imghead>';
var img = document.getElementById('imghead');
img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
status = ('rect:' + rect.top + ',' + rect.left + ',' + rect.width + ',' + rect.height);
div.innerHTML = "<div id=divhead style='width:" + rect.width + "px;height:" + rect.height + "px;margin-top:" + rect.top + "px;margin-left:" + rect.left + "px;" + sFilter + src + "\"'></div>";
}
}
function previewImage1(file) {
var MAXWIDTH = 860;
var MAXHEIGHT = 120;
var div = document.getElementById('preview1');
if (file.files && file.files[0]) {
div.innerHTML = '<img id=imghead1>';
var img = document.getElementById('imghead1');
img.onload = function () {
var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
img.width = rect.width;
img.height = rect.height;
img.style.marginLeft = rect.left + 'px';
img.style.marginTop = rect.top + 'px';
}
var reader = new FileReader();
reader.onload = function (evt) { img.src = evt.target.result; }
reader.readAsDataURL(file.files[0]);
}
else {
var sFilter = 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
file.select();
var src = document.selection.createRange().text;
div.innerHTML = '<img id=imghead1>';
var img = document.getElementById('imghead1');
img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
status = ('rect:' + rect.top + ',' + rect.left + ',' + rect.width + ',' + rect.height);
div.innerHTML = "<div id=divhead style='width:" + rect.width + "px;height:" + rect.height + "px;margin-top:" + rect.top + "px;margin-left:" + rect.left + "px;" + sFilter + src + "\"'></div>";
}
}
function clacImgZoomParam(maxWidth, maxHeight, width, height) {
var param = { top: 0, left: 0, width: width, height: height };
if (width > maxWidth || height > maxHeight) {
rateWidth = width / maxWidth;
rateHeight = height / maxHeight;
if (rateWidth > rateHeight) {
param.width = maxWidth;
param.height = Math.round(height / rateWidth);
} else {
param.width = Math.round(width / rateHeight);
param.height = maxHeight;
}
}
param.left = Math.round((maxWidth - param.width) / 2);
param.top = Math.round((maxHeight - param.height) / 2);
return param;
}
</script>
<img alt="" id="imghead" runat="server" style="height:120px"/>
<input type="file" runat="server" id="fileup1" onchange="previewImage(this)" />