文件上传器 [英] FileUploader
本文介绍了文件上传器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我是将文件上传到名为"UploadedImages"的文件夹,并且一切都很好,但是我有一个图像控件,我希望它显示图像,但它没有做,知道在我上传图像时,图像上的取消"按钮变成另一个图标.这是我的代码.希望任何人都可以帮助我.
Hi,iam uploading files to a folder called "UploadedImages",and everything is going fine,but i have a an image control which i want it to show the image but it is not doing,knowing that when i upload the image,the cancel button on the image turns into another icon.This is my code.I hope that anyone help me.
protected void Button1_Click(object sender, EventArgs e)
{
if (IsPostBack)
{
Boolean fileOK = false;
String path = Server.MapPath("UploadedImages");
if (FileUpload1.HasFile)
{
String fileExtension =
System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}
if (fileOK)
{
try
{
FileUpload1.PostedFile.SaveAs(path
+ FileUpload1.FileName);
Label1.Text = "File uploaded!";
Image1.ImageUrl = path + FileUpload1.FileName;
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}
else
{
Label1.Text = "Cannot accept files of this type.";
}
}
}
推荐答案
Image1.ImageUrl将采用〜/MY_IMAGE.JPG"之类的相对Url,因此在将相对URl上传到图像控件后,就可以解决问题.
希望这会有所帮助.
Hi,
Image1.ImageUrl will take relative Url like "~/MY_IMAGE.JPG" so after uploading assign relative URl to your image control, that will do the trick.
Hope this will help.
尝试一下,
Try this,
Image1.ImageUrl = "~/UploadedImages/" + FileUpload1.FileName;
这里是一个示例工作代码段.您必须使用ImageURL提供相对路径.
Here is one sample working code snippet. You have to provide Relative paths with ImageURL.
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string filename = Guid.NewGuid().ToString() + ".jpg";
FileUpload1.PostedFile.SaveAs(Server.MapPath("Images\\") +filename);
aspimage.ImageUrl = "~/Images/" + filename;
}
}
这篇关于文件上传器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文