asp.net怎么把页面上的保存图片到指定文件夹中?

caicainiao1 2012-04-06 04:31:09
在我的表里面有个存储图片的路径,项目里面有个存储图片的文件夹,我现在要把这个路径上的图片保存到指定的文件夹下。该怎么用代码实现啊?请求各位朋友帮忙解决啊。
...全文
288 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
caicainiao1 2012-04-06
  • 打赏
  • 举报
回复
我最上面写着了,晕了
happytonice 2012-04-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

我是要把从数据库中取图片放到网页上,再从网页上把图片 保存 到 指定 的文件夹中。
[/Quote]

费那么大劲,转的弯不少
caicainiao1 2012-04-06
  • 打赏
  • 举报
回复
我是要把从数据库中取图片放到网页上,再从网页上把图片 保存 到 指定 的文件夹中。
yangchenhuahua 2012-04-06
  • 打赏
  • 举报
回复
正解:

public ActionResult Upload(int? id, HttpPostedFileBase pic,string TS)
{
if (id.HasValue)
{
string[] picExt = { "gif", "png", "jpg", "jpeg" };
int maxLength = 1024 * 1024 * 10;
if (pic != null)
{

if (pic.ContentLength > maxLength || !picExt.Contains(pic.FileName.ToLower().GetFileExtentionName().Replace(".", "")))
{
return JsonText(new { OK = false, Error = "图片超过大小或格式不对!" });
}
else
{
if (string.IsNullOrWhiteSpace(TS))
{
return JsonText(new { OK = false, Error = "非法操作!" });
}
byte[] ts = HttpUtility.UrlDecodeToBytes(TS);
UserSessionModel user = UserInfo;

//zhangji 2012-03-13 changed "(a.FileState == null || a.FileState == 0)" to "(a.FileState == null || a.FileState == 0 || a.FileState == 2)"
T_Order order = EF.T_Order.Where(a => a.ID == id.Value && a.TS == ts && (a.FileState == null || a.FileState == 0 || a.FileState == 2) && (a.Company_ID == user.CompanyID || user.IsAdmin)).SingleOrDefault();
if (order == null)
{
return JsonText(new { OK = false, Error = "操作失败!数据已被修改,请重新查看!" });
}
order.UploadCount = order.UploadCount + 1;
order.FilePath = id.ToString() + pic.FileName.ToLower().GetFileExtentionName();
string path = Server.MapPath("~/App_Data/Upload/") + order.FilePath;
pic.SaveAs(path);
try {
EF.SaveChanges();
}
catch (OptimisticConcurrencyException)
{
return JsonText(new { OK = false, Error = "操作失败!数据已被修改,请重新查看!" });
}
return JsonText(new { OK = true, Path = order.FilePath, ID = id ,Flag=order.UploadCount<3});
}
}
else
{
return JsonText(new { OK = false, Error = "请选择图片!" });
}
}
else
{
return JsonText(new { OK = false, Error = "缺少参数!" });
}
}
  • 打赏
  • 举报
回复
在保证有读写权限的情况下

public ActionResult Upload(int? id, HttpPostedFileBase pic,string TS)
{
if (id.HasValue)
{
string[] picExt = { "gif", "png", "jpg", "jpeg" };
int maxLength = 1024 * 1024 * 10;
if (pic != null)
{

if (pic.ContentLength > maxLength || !picExt.Contains(pic.FileName.ToLower().GetFileExtentionName().Replace(".", "")))
{
return JsonText(new { OK = false, Error = "图片超过大小或格式不对!" });
}
else
{
if (string.IsNullOrWhiteSpace(TS))
{
return JsonText(new { OK = false, Error = "非法操作!" });
}
byte[] ts = HttpUtility.UrlDecodeToBytes(TS);
UserSessionModel user = UserInfo;

//zhangji 2012-03-13 changed "(a.FileState == null || a.FileState == 0)" to "(a.FileState == null || a.FileState == 0 || a.FileState == 2)"
T_Order order = EF.T_Order.Where(a => a.ID == id.Value && a.TS == ts && (a.FileState == null || a.FileState == 0 || a.FileState == 2) && (a.Company_ID == user.CompanyID || user.IsAdmin)).SingleOrDefault();
if (order == null)
{
return JsonText(new { OK = false, Error = "操作失败!数据已被修改,请重新查看!" });
}
order.UploadCount = order.UploadCount + 1;
order.FilePath = id.ToString() + pic.FileName.ToLower().GetFileExtentionName();
string path = Server.MapPath("~/App_Data/Upload/") + order.FilePath;
pic.SaveAs(path);
try {
EF.SaveChanges();
}
catch (OptimisticConcurrencyException)
{
return JsonText(new { OK = false, Error = "操作失败!数据已被修改,请重新查看!" });
}
return JsonText(new { OK = true, Path = order.FilePath, ID = id ,Flag=order.UploadCount<3});
}
}
else
{
return JsonText(new { OK = false, Error = "请选择图片!" });
}
}
else
{
return JsonText(new { OK = false, Error = "缺少参数!" });
}
}

62,046

社区成员

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

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

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

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