请帮我展示所有扩展的图片 [英] please help me to show picture with all extension

查看:37
本文介绍了请帮我展示所有扩展的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是图片上传代码。问题是在page_load,我必须提供扩展名.jpg来显示图片。但它只显示.jpg图片,但我想显示所有扩展名的图片。

如果我删除.jpg没有显示任何内容..

请帮我显示所有扩展名的图片









 受保护  void  Page_Load( object  sender,EventArgs e)
{
// Session.Timeout = 90;

string 用户名=( string )(会话[ UserAuthentication]);
if (会话[ UserAuthentication]!= null
{
Label2.Text = username;

Image1.ImageUrl = Images / + username + 。jpg;


}





  private   void  StartUpLoad()
{
// 获取已发布图像的文件名
string username =( string )(会话[ UserAuthentication]);

// 获取字节大小

int imgSize = FileUpload1.PostedFile.ContentLength;

// 在保存之前验证已发布的文件
if (FileUpload1.PostedFile!= null && FileUpload1.PostedFile.FileName!=
{
// 10240 KB表示10MB,您可以根据需求更改值
如果(FileUpload1 .PostedFile.ContentLength > 1024000
{
Page.ClientScript.RegisterClientScriptBlock ( typeof (页面), 警告 aler t('文件太大。') true );

}
else
{
// 然后将其保存到文件夹
// string [] splitter = FileUpload1.PostedFile.FileName.Split('。');
string ext = System.IO。 Path.GetExtension(FileUpload1.PostedFile.FileName);
string filePath = Images / + username + ext; // +。 + splitter [1] .ToString();
FileUpload1.SaveAs(Server.MapPath(filePath));
Image1.ImageUrl = 〜/ + filePath;
Page.ClientScript.RegisterClientScriptBlock( typeof (页面), Alert alert('Image saved!') true );

}

}


}





  protected   void  Button2_Click1( object  sender,EventArgs e)
{

StartUpLoad();

}

解决方案

尝试存储带扩展名的图像名

例如。 summer.jpg,winter.gif



然后被绑定到图片网址,不要在这里添加任何扩展名


我是完全赞同Sergery。这是不是一个好习惯。在这种情况下,您可以将文件存储在文件夹中并将URL存储在数据库中。显示时,您可以从数据库中获取图像路径并将其分配给图像URL。参见示例:

如何将图像保存到数据库中的文件夹和图像路径中,并使用asp.net 基于数据库中的图像路径显示gridview中文件夹中的图像 [ ^ ]



查看您的临时解决方案:

如果用户名是唯一的,图像将以唯一名称存储。您所要做的就是使用用户名搜索文件并将路径指定为图像的URL。试试这个:

 受保护  void  Page_Load ( object  sender,EventArgs e)
{
string username =( string )(会话[ UserAuthentication]) ;
if (会话[ UserAuthentication]!= null
{
Label2.Text = username;
// 获取存储在images目录中的所有图像
string [] filePaths = Directory.GetFiles(Server.MapPath( 〜/图像/));
// 搜索与用户名相同的图片
string path = filePaths.AsEnumerable()。Where(s = > s.Contains(username))。First() ;
// 分配路径
Image1.ImageUrl = path;
}
}







- Amit

this is image upload code.problem is on page_load where i have to give extension ".jpg" to show the picture.but it only shows .jpg picture but i want to show picture with all the extension.
If i remove .jpg is shows nothing..
please help me to show picture with all extension




protected void Page_Load(object sender, EventArgs e)
       {
           //Session.Timeout = 90;

           string username = (string)(Session["UserAuthentication"]);
               if(Session["UserAuthentication"]!=null)
               {
                   Label2.Text = username;

                   Image1.ImageUrl = "Images/" + username + ".jpg";


               }



private void StartUpLoad()
       {
           //get the file name of the posted image
              string username = (string)(Session["UserAuthentication"]);

           //get the size in bytes that

           int imgSize = FileUpload1.PostedFile.ContentLength;

           //validates the posted file before saving
           if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
           {
               // 10240 KB means 10MB, You can change the value based on your requirement
               if (FileUpload1.PostedFile.ContentLength > 1024000)
               {
                   Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true);

               }
               else
               {
                   //then save it to the Folder
                //string[] splitter = FileUpload1.PostedFile.FileName.Split('.');
                   string ext=System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
                   string filePath = "Images/" + username+ext; //+"." + splitter[1].ToString();
                FileUpload1.SaveAs(Server.MapPath(filePath));
                Image1.ImageUrl = "~/" + filePath;
                Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Image saved!')", true);

               }

           }


        }



protected void Button2_Click1(object sender, EventArgs e)
       {
           
           StartUpLoad();
          
       }

解决方案

try to store imagename with extension
eg. summer.jpg,winter.gif
and
then assiged to image url and don't metion any extension here


I am totally agree with Sergery. This is "Not a good practice". In case of that, you can store the files in folder and store the url in database. While displaying, you can take image path from database and assign it to Image URL. See example:
how to save images into folder and images path in database and display images from folder in gridview based on images path in database using asp.net[^]

see your temporary solution:
If username is unique, the images will store with unique name. All you'll have to do is, search the file with username and assign the path as URL of image. Try this:

protected void Page_Load(object sender, EventArgs e)
{
    string username = (string)(Session["UserAuthentication"]);
    if(Session["UserAuthentication"]!=null)
    {
       Label2.Text = username;
       //Get all the images stored in images directory
       string[] filePaths = Directory.GetFiles(Server.MapPath("~/Images/"));
       //Search the image with same name as username
       string path = filePaths.AsEnumerable().Where(s => s.Contains(username)).First();
       //Assign the path
       Image1.ImageUrl = path;
    }
}




--Amit


这篇关于请帮我展示所有扩展的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆