ASP.NET中FileUpload中的代码怎么编写?
点击浏览后有个上传按钮,上传到我建的FileUploda文件夹下,这个FileUploda控件里面的代码要怎么实现?...
点击浏览后有个上传按钮,上传到我建的FileUploda文件夹下,这个FileUploda控件里面的代码要怎么实现?
展开
展开全部
<asp:FileUpload ID="fuPhoto" onchange="javascript:__doPostBack('lbUploadPhoto','')"
runat="server" ToolTip="选择图片" />
<asp:LinkButton ID="lbUploadPhoto" runat="server" OnClick="lbUploadPhoto_Click"></asp:LinkButton>
后台代码:
//自动上传事件
protected void lbUploadPhoto_Click(object sender, EventArgs e)
{
fileUpload();
}
//从控件上传文件
public void fileUpload()
{
if (fuPhoto.PostedFile != null && fuPhoto.PostedFile.ContentLength > 0)
{
string ext = System.IO.Path.GetExtension(fuPhoto.PostedFile.FileName).ToLower();
if (ext != ".jpg" && ext != ".jepg" && ext != ".bmp" && ext != ".gif")
{
return;
}
string filename = "Image_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ext;
string path = "./UploadPhoto/" + filename;
fuPhoto.PostedFile.SaveAs(Server.MapPath(path));
Response.Redirect("ImageCut.aspx?Picurl=" + Server.UrlEncode(path));
}
else
{
//do some thing;
}
}
runat="server" ToolTip="选择图片" />
<asp:LinkButton ID="lbUploadPhoto" runat="server" OnClick="lbUploadPhoto_Click"></asp:LinkButton>
后台代码:
//自动上传事件
protected void lbUploadPhoto_Click(object sender, EventArgs e)
{
fileUpload();
}
//从控件上传文件
public void fileUpload()
{
if (fuPhoto.PostedFile != null && fuPhoto.PostedFile.ContentLength > 0)
{
string ext = System.IO.Path.GetExtension(fuPhoto.PostedFile.FileName).ToLower();
if (ext != ".jpg" && ext != ".jepg" && ext != ".bmp" && ext != ".gif")
{
return;
}
string filename = "Image_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ext;
string path = "./UploadPhoto/" + filename;
fuPhoto.PostedFile.SaveAs(Server.MapPath(path));
Response.Redirect("ImageCut.aspx?Picurl=" + Server.UrlEncode(path));
}
else
{
//do some thing;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
if (UpLoadFile1.PostedFile != null) { try { string strFile = UpLoadFile1.PostedFile.FileName; //string fex = Path.GetExtension(UpLoadFile1.PostedFile.FileName); if (strFile.Substring(strFile.LastIndexOf('.') + 1).ToLower() != "jpg" && strFile.Substring(strFile.LastIndexOf('.') + 1).ToLower() != "jpeg" && strFile.Substring(strFile.LastIndexOf('.') + 1).ToLower() != "gif" && strFile.Substring(strFile.LastIndexOf('.') + 1).ToLower() != "bmp") { Label4.Text = "只能上传 jpg,jpeg,gif,和bmp 格式的照片!"; } else { Random ro = new Random(); string stro = ro.Next(100, 100000000).ToString() ;//产生一个随机数用于新命名的图片 string NewName = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString()+ stro; string kz = strFile.Substring(strFile.LastIndexOf('.')); string fileSavePath = Server.MapPath("./flash/") + NewName+kz; UpLoadFile1.PostedFile.SaveAs(fileSavePath); string url = "admin/flash/" + NewName+kz; string name = Server.HtmlEncode(TextBox1.Text.Trim()); string content = Server.HtmlEncode(TxtContent.Text.Trim().Replace("'", "")); DateTime dtt = DateTime.Now; int id = Convert.ToInt32(DropDownList2.SelectedValue); sc.ExecSql("insert into [ilev3]([name],[content],[photo],[dtt],[ilev2id])values('" + name + "','" + content + "','" + url + "','" + dtt + "'," + id + ")"); Response.Write("<script language='javascript'>alert('添加信息成功!');</script>"); } } catch { Response.Write("<script >alert('上传照片出错!请重试!');</script>"); } }
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
protected void submit_Click(object sender, EventArgs e)
{
string name = FileUpload1.FileName;
int size = FileUpload1.PostedFile.ContentLength;
string type = FileUpload1.PostedFile.ContentType; //image/pjpeg
// string type2 = name.Substring(name.LastIndexOf(".") + 1); //jpg 不安全
string ipath = Server.MapPath(@"~\img\") + name;
string dpath = @"~\img\" + name;
if (type.Contains("image"))
{
if (size < 1048576) // 1MB
{
FileUpload1.SaveAs(ipath);
Image1.Visible = true;
Image1.ImageUrl = dpath;
Response.Write("恭喜你上传成功!");
}
else
{
Response.Write("图片过大!请上传小于1MB的图片");
}
}
else
{
Response.Write("请上传正确的图片格式");
}
}
{
string name = FileUpload1.FileName;
int size = FileUpload1.PostedFile.ContentLength;
string type = FileUpload1.PostedFile.ContentType; //image/pjpeg
// string type2 = name.Substring(name.LastIndexOf(".") + 1); //jpg 不安全
string ipath = Server.MapPath(@"~\img\") + name;
string dpath = @"~\img\" + name;
if (type.Contains("image"))
{
if (size < 1048576) // 1MB
{
FileUpload1.SaveAs(ipath);
Image1.Visible = true;
Image1.ImageUrl = dpath;
Response.Write("恭喜你上传成功!");
}
else
{
Response.Write("图片过大!请上传小于1MB的图片");
}
}
else
{
Response.Write("请上传正确的图片格式");
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这是上传图片的,你可以改成自己想要的格式,也看添加别的格式
string imgname = this.File1.PostedFile.FileName.Substring(this.File1.PostedFile.FileName.LastIndexOf('\\') + 1);
string Extension = System.IO.Path.GetExtension(imgname).ToLower();
if (Extension == ".gif" || Extension == ".jpg")
{
if (File1.PostedFile.ContentLength > 1024000)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传图片的大小不能超过1M!')</script>");
}
else
{
string newname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + Extension;
System.Drawing.Image img = System.Drawing.Image.FromStream(File1.PostedFile.InputStream);
img.Save(Server.MapPath("~/uploads/" + newname), System.Drawing.Imaging.ImageFormat.Jpeg);
this.upimage.Text = newname;
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传图片格式错误')</script>");
}
string imgname = this.File1.PostedFile.FileName.Substring(this.File1.PostedFile.FileName.LastIndexOf('\\') + 1);
string Extension = System.IO.Path.GetExtension(imgname).ToLower();
if (Extension == ".gif" || Extension == ".jpg")
{
if (File1.PostedFile.ContentLength > 1024000)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传图片的大小不能超过1M!')</script>");
}
else
{
string newname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + Extension;
System.Drawing.Image img = System.Drawing.Image.FromStream(File1.PostedFile.InputStream);
img.Save(Server.MapPath("~/uploads/" + newname), System.Drawing.Imaging.ImageFormat.Jpeg);
this.upimage.Text = newname;
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传图片格式错误')</script>");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询