求html+ashx 上传图片的实例,谢谢

cn_free 2012-02-25 11:33:03
要实例...
很急...
...全文
371 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
newdigitime 2012-02-26
  • 打赏
  • 举报
回复

<form method="post" enctype="multipart/form-data" action="picup.ashx">
<input type="file" />
<input type="submit" value="上传">
</form>



<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class Handler : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
HttpFileCollection hfc=context.Request.Files;
HttpPostedFile upfile = hfc[0];
upfile.SaveAs(context.Server.MapPath("test.file"));
if (hfc[0].InputStream!=null) hfc[0].InputStream.Close();
}
public bool IsReusable
{
get {return true;}
}
}

我就是大神 2012-02-26
  • 打赏
  • 举报
回复
操作的内容还得ashx写啊。input file只是获得个路径而已。ashx根据路径上传到某个文件夹啊。

form表单加个最后那个属性。
<form action="03-PhotoAdd.ashx" method="post" enctype="multipart/form-data">

前面还得加个隐藏标签,起个name=viewstate
input标签。
<input type="text" name="txtDes" value=" " />
<input type="submit" value="上传" />

ashx写。
string viewstate = context.Request.Form["viewstate"];
if (!string.IsNullOrEmpty(viewstate))
{
string path = context.Request.Form["txtDes"];

HttpPostedFile file = null;
if (context.Request.Files.Count > 0)
{
file = context.Request.Files[0];
下面这是起个随机名字而已
Random r = new Random();
string filename = DateTime.Now.ToString("yyyyMMddhhmmss") + r.Next(10000, 100000) + System.IO.Path.GetExtension(file.FileName);

string filepath = context.Request.MapPath("imgs/" + filename ); imgs只是网站里的文件夹,我是用来存上传的图片的
file.SaveAs(filepath);
}
else
{
//MessageBox.Show(context, "请上传文件");
//context.Response.Write(html);
}

可能还会有点错,大概就是这样
wongeo 2012-02-26
  • 打赏
  • 举报
回复
http://download.csdn.net/detail/wildfeng04/4092050
这个里面是源代码
wongeo 2012-02-26
  • 打赏
  • 举报
回复
<form action="exec/Upload.ashx" enctype="multipart/form-data" method="post">
<input name="file" type="file" />
<input type="submit" value="提交" />
</form>

aspx页面写这些。

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
HttpFileCollection imgs = context.Request.Files;
if (imgs.Count>0)
{
for (int i = 0; i < imgs.Count; i++)
{
if (imgs[i].ContentLength!=0)
{
string oldName=imgs[i].FileName.ToString();
string imgName = Guid.NewGuid().ToString();
string imgType = oldName.ToString().Substring(oldName.LastIndexOf("."));
string imgPath = "~/upload/" + imgName + imgType;
imgs[i].SaveAs(context.Server.MapPath(imgPath));
}
}
}
}

一般处理程序写这些

exec/Upload.ashx
upload
Default.aspx
文件目录
cn_free 2012-02-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chinajiyong 的回复:]

http://www.yongfa365.com/Item/WinForm-Uploader.ashx.html
[/Quote]
这个看不懂额。用input type=file上传怎么上传?

62,244

社区成员

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

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

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

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