ASP.NET中FileUpload中的代码怎么编写?

2024-12-28 11:29:57
推荐回答(4个)
回答1:

runat="server" ToolTip="选择图片" />



后台代码:

//自动上传事件
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;
}
}

回答2:

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(""); } } catch { Response.Write(""); } }

回答3:

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("请上传正确的图片格式");
}

}

回答4:

这是上传图片的,你可以改成自己想要的格式,也看添加别的格式
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(), "", "");
}
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(), "", "");
}