Visual Studio中的FileUpload控件将图像保存到MySql数据库

时间:2016-12-11 07:35:24

标签: c# mysql mysql-workbench

如何使用fileupload控件上传图像并将其保存在MySQL工作台数据库的表中?以及如何创建连接。 表格的名称是invoicesfp

import ma.glasnost.orika.BoundMapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.converter.ConverterFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;

public class EntryClass {

    public static void main(String[] args) {
        EntryClass ec = new EntryClass();
        BoundMapperFacade<A, B> facade = getMapperFactory().getMapperFacade(A.class, B.class);
        A fahr = new A(455);
        B cels = facade.map(fahr);
        System.out.println(cels);
        A revFahr = facade.mapReverse(cels);
        System.out.println(revFahr);
    }

    private static MapperFactory getMapperFactory() {
        MapperFactory factory = new DefaultMapperFactory.Builder()
                                .build();
        ConverterFactory cfactory = factory.getConverterFactory();
        cfactory.registerConverter(new FahrCelsConverter());
        factory.classMap(A.class, B.class)
                .field("fahr", "cels")
                .byDefault()
                .register();
        return factory;
    }
}

public class A {

    long fahr;

    public A(long fahr) {
        this.fahr = fahr;
    }


    public long getFahr() {
        return fahr;
    }


    public void setFahr(long fahr) {
        this.fahr = fahr;
    }


    @Override
    public String toString() {
        return "A [fahr=" + fahr + "]";
    }
}

public class B {

    long cels;

    public B(long cels) {
        this.cels = cels;
    }

    public long getCels() {
        return cels;
    }

    public void setCels(long cels) {
        this.cels = cels;
    }

    @Override
    public String toString() {
        return "B [cels=" + cels + "]";
    }
}

public class FahrCelsConverter extends BidirectionalConverter<A, B>
{
@Override
public B convertTo(A source, Type<B> destinationType, MappingContext mappingContext) {      
    if(source != null)
    {
        return new B((source.fahr - 32) * 5 / 9);
    }
    return null;
}

@Override
public A convertFrom(B source, Type<A> destinationType, MappingContext mappingContext) {
    if(source != null)
    {
        return new A((source.cels / 5) * 9 + 32);
    }
    return null;
}
}

1 个答案:

答案 0 :(得分:1)

所以这是我为您的保存图像编写的代码,您必须在应用程序的根目录中创建一个文件夹,例如upload

            #region fileupload
            string fn = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
            string ret = Rename.ChangeName();
            string SaveLocation = Server.MapPath("Upload") + "\\" + ret;

            try
            {
                FileUpload1.PostedFile.SaveAs(SaveLocation);
            }
            catch (Exception ex)
            {
                if (ex is ArgumentNullException || ex is NullReferenceException)
                {
                    throw ex; 
                }
            }
            string PicAddress = "~/Upload/" + ret;

            #endregion

你可以看到有一个更改名称的方法,假设你只想保存jpg文件,对于其他你可以使用扩展名的文件:

    public static string ChangeName()
    {
        return Guid.NewGuid().ToString("N") + ".jpg";
    }

在这些代码运行之后,您可以轻松地将PicAddress作为字符串保存到数据库中,因此请为其创建示例nvarchar字段。只要您想要显示图像,您只需要将图像的地址引用到图像标记:

<img src="~/Upload/etcetc.jpg">