上传的图片名称变更 [英] changing name of uploaded images

查看:124
本文介绍了上传的图片名称变更的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所试图实现的是改变上传的图片的名称有点唯一的字符串。

What I am trying to achieve is changing the name of uploaded images to a somewhat unique string.

我创建了由数据库动态填充一个图片库。画廊工作正常但,如果用相同名称的影像上传存储在数据库中的文件将是有问题的!

I have created an image gallery which is populated dynamically by database. The gallery is working fine except, that files stored in the database will be problematic if an image with the same name is uploaded!

code控制器中的文件上传:

Code in the controller for file upload:

string AdvertImage = picture1.FileName;
                    advert.AdvertImage = AdvertImage;
                    var image1Path = Path.Combine(Server.MapPath("~/Content/Images"), AdvertImage);
                    picture1.SaveAs(image1Path);

在code以下就是我对

The code below is what I am working on

                    string BackImage = DateTime.Now.ToString("yyyyMMddhhmmssfffffff");
                    caradvert.BackImage = BackImage;
                    var image3Path = Path.Combine(Server.MapPath("~/Content/CarImages"), BackImage);
                    picture3.SaveAs(image3Path);

我设法创建一个唯一的文件名,你就不会追加.JPG到图像的结尾。

I have managed to create a unique file name thou it is not appending .jpg to the end of the image.

在主题欢迎任何帮助或建议

Any help or advice on the subject welcome

推荐答案

试试这个:

        //save file in folder
    if (FileUpload1.PostedFile.ContentType.ToLower().StartsWith
            ("image") && FileUpload1.HasFile)
    {
        string savelocation = Server.MapPath("savedImages/");
        string fileExtention = System.IO.Path.GetExtension(FileUpload1.FileName);
        //creating filename to avoid file name conflicts.
        string fileName = Guid.NewGuid().ToString();
        //saving file in savedImage folder.
        string savePath = savelocation + fileName + fileExtention;
        FileUpload1.SaveAs(savePath);
    }

这篇关于上传的图片名称变更的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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