【100分】求多图上传+预览

chai1338 2010-09-27 11:02:38
RT。。有预览没上传的别发
有上传没预览的别发
不是多图的别发。
因为我找了很多案例了。没一个完全符合的。
有能力的修改下面的也行
http://topic.csdn.net/u/20100927/10/390c0606-54cd-4a4a-a8ee-ea92923b4f4f.html?54849
...全文
753 38 打赏 收藏 转发到动态 举报
写回复
用AI写文章
38 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2010-09-27
  • 打赏
  • 举报
回复
<img src="data:image/jpeg;base64"这样的写法,IE7之前不支持,
既然都已经上传了,那就直接保存下来就是了。

预览的方法你可以参考这个

http://www.cnblogs.com/cloudgamer/archive/2009/12/22/ImagePreview.html
chai1338 2010-09-27
  • 打赏
  • 举报
回复
孟哥。先吃饭吧。吃完再帮我看看
chai1338 2010-09-27
  • 打赏
  • 举报
回复
我发了例子 不限麻烦的话 可以看下源码
chai1338 2010-09-27
  • 打赏
  • 举报
回复
ImagePreview.ashx

<%@ WebHandler Language="c#" Class="File_WebHandler" Debug="true" %>

using System;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

public class File_WebHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.Request.Files.Count > 0)
{
HttpPostedFile file = context.Request.Files[0];

if (file.ContentLength > 0 && file.ContentType.IndexOf("image/") >= 0)
{
int width = Convert.ToInt32(context.Request.Form["width"]);
int height = Convert.ToInt32(context.Request.Form["height"]);

string path = "data:image/jpeg;base64," + Convert.ToBase64String(ResizeImg(file.InputStream, width, height).GetBuffer());

context.Response.Write(path);
}
}
}

public MemoryStream ResizeImg(Stream ImgFile, int maxWidth, int maxHeight)
{
Image imgPhoto = Image.FromStream(ImgFile);

decimal desiredRatio = Math.Min((decimal)maxWidth / imgPhoto.Width, (decimal)maxHeight / imgPhoto.Height);
int iWidth = (int)(imgPhoto.Width * desiredRatio);
int iHeight = (int)(imgPhoto.Height * desiredRatio);

Bitmap bmPhoto = new Bitmap(iWidth, iHeight);

Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
gbmPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(0, 0, imgPhoto.Width, imgPhoto.Height), GraphicsUnit.Pixel);

MemoryStream ms = new MemoryStream();
bmPhoto.Save(ms, ImageFormat.Jpeg);

imgPhoto.Dispose();
gbmPhoto.Dispose();
bmPhoto.Dispose();

return ms;
}

public bool IsReusable
{
get
{
return false;
}
}
}
孟子E章 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 chai1338 的回复:]

JS动态添加input控件 类型为file name为pic。后台遍历的时候应该不用调用name吧
[/Quote]
表单都是靠name提交的,没有name是不会提交服务器上去的
孟子E章 2010-09-27
  • 打赏
  • 举报
回复
action: "ImagePreview.ashx",
你的预览是先传到服务器上的吧?要不然怎么会有这行?
mark620 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 chai1338 的回复:]
检查了 放在form里面
添加是这样的

C# code

<table class="perview">
<tr>
<th align="right"> 选择图片: </th>
<td width="75%"> <div id="idPicFile"> </div> </td>
</tr>
<tr>
<……
[/Quote]

好的很
chai1338 2010-09-27
  • 打赏
  • 举报
回复
JS动态添加input控件 类型为file name为pic。后台遍历的时候应该不用调用name吧
chai1338 2010-09-27
  • 打赏
  • 举报
回复
检查了 放在form里面
添加是这样的

<table class="perview">
<tr>
<th align="right"> 选择图片: </th>
<td width="75%"> <div id="idPicFile"> </div> </td>
</tr>
<tr>
<td colspan="2"><table>
<thead>
<tr>
<th> 文件路径 </th>
<th width="30%"> 预览图 </th>
<th width="20%"> 操作 </th>
</tr>
</thead>
<tbody id="idPicList">
<tr>
<td></td>
<td align="center"></td>
<td align="center"><a href="#">移除</a></td>
</tr>
</tbody>
</table></td>
</tr>
</table>
<script>

var table = $$("idPicList"), model = table.removeChild(table.rows[0]);

function AddPreview(){
var file = document.createElement("input"),
img = document.createElement("img"),
ip = new ImagePreview( file, img, {
maxWidth: 150,
maxHeight: 100,
action: "ImagePreview.ashx",
onErr: function(){ alert("载入预览出错!"); ResetFile(file); },
onCheck: CheckPreview,
onShow: ShowPreview
});
file.type = "file"; file.name = "pic";
file.onchange = function(){ ip.preview(); };
$$("idPicFile").appendChild(file);
}

孟子E章 2010-09-27
  • 打赏
  • 举报
回复
页面有form吗?前端你怎么添加的type=file?
一定要放在form里面,一定要有name属性
chai1338 2010-09-27
  • 打赏
  • 举报
回复
不管我在前台添加了几个。后台的files.Count就是等于0
chai1338 2010-09-27
  • 打赏
  • 举报
回复
你看吧 都一模一样的 我只是把

System.Text.StringBuilder strMsg = new System.Text.StringBuilder("您输入的用户名是:" + TextBox1.Text + "<br/>");
strMsg.Append("上传的文件分别是:<hr color='red'/>");
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + fileName + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");

去掉了
chai1338 2010-09-27
  • 打赏
  • 举报
回复

我在预览完之后 有个button上传按钮点击事件
string url = "";
//遍历file表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
for (int iFile = 0; iFile < files.Count; iFile++)
{

//检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);

///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
string fileUrlPath = "~/admin/computerImgs/";//生成的文件夹要保存的位置
string time = DateTime.Now.ToString("yyyyMMdd");
string path = fileUrlPath + time + "/"; //完整路径
if (!Directory.Exists(Server.MapPath(path)))
{
Directory.CreateDirectory(Server.MapPath(path)); //创建文件夹
}

string imgUrl = path + fileName;

url += imgUrl + "|";

postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(path) + fileName);
}
}

这样取不到。利用的是超链接里的列子
孟子E章 2010-09-27
  • 打赏
  • 举报
回复
http://dotnet.aspx.cc/article/58ea3515-36f2-4fd9-ac89-eaf49f59816c/read.aspx

遍历files表单元素的方法
chai1338 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 sq_zhuyi 的回复:]

http://topic.csdn.net/u/20100926/10/ab263be2-79b5-49b7-a256-870a37185c9c.html
[/Quote]

这个也是我发的帖子
孟子E章 2010-09-27
  • 打赏
  • 举报
回复
你使用的是<input type=file name=f>吗?怎么可能取不到呢?

上传之前是不能刷新页面的。
chai1338 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 net_lover 的回复:]

既然单独预览可以实现,单独的上传可以实现,为啥你预览+上传就不会了呢?作为搞程序的,自己一点研究的心思都没有吗?

预览完毕之后,点击按钮上传就可以了,
[/Quote]
我研究了啊~
我在后台遍历files表单元素 取不到东西~
我要是没研究我会这样说吗
孟子E章 2010-09-27
  • 打赏
  • 举报
回复
既然单独预览可以实现,单独的上传可以实现,为啥你预览+上传就不会了呢?作为搞程序的,自己一点研究的心思都没有吗?

预览完毕之后,点击按钮上传就可以了,
路人乙e 2010-09-27
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20100926/10/ab263be2-79b5-49b7-a256-870a37185c9c.html
Hertz_liu 2010-09-27
  • 打赏
  • 举报
回复
使用SwfUpload吧,挺好用的,可以实现你的功能。可以到网上搜搜看,具体的案例现在我没有
加载更多回复(16)

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧