在C#的CS结构中,怎么实现拖拽多个文件,实现把本地文件上传到数据库中,有控件么?求高手指点下。

在C#的CS结构中,怎么实现拖拽多个文件,实现把本地文件上传到数据库中,有控件么?求高手指点下。,第1张

首先设置Form的AllowDrop=true

public partial class Form1 : Form

{

    public Form1()

    {

        InitializeComponent()

    }

    private void btnStart_Click(object sender, EventArgs e)

    {

    }

    private void Form1_DragEnter(object sender, DragEventArgs e)

    {

        if (e.Data.GetDataPresent(DataFormats.FileDrop))

        {

            e.Effect = DragDropEffects.Link 

        }

        else

        {

            e.Effect = DragDropEffects.None

        }

    }

    private void Form1_DragDrop(object sender, DragEventArgs e)

    {

        string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString()

        // 接下来可以通过filestream来上传文件

    }

}

在DragDrop事件中能够得到拖放到窗体上的文件路径,然后使用filestream就可以上传了。

//文件写入流

private void ReadFile()

{

Byte[] MesageFile

string path =@"c:\123.XML"

FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read)

int size = Convert.ToInt32(stream.Length)

MesageFile = new Byte[size]

stream.Read(MesageFile, 0, size)

stream.Close()

string fileName =path.Substring(path.LastIndexOf("\\") + 1, path.Length path.LastIndexOf("\\") - 1)

WriteFile(MesageFile, fileName)

}

//写入文件

private void WriteFile(Byte[] fileByte,string fileName)

{

string path = AppDomain.CurrentDomain.BaseDirectory + "\\UpLoad\\" + DateTime.Now.ToString("yyyy-MM-dd")+"\\"

if (!Directory.Exists(path))

Directory.CreateDirectory(path)

string savepath = path + fileName

FileStream fos = new FileStream(savepath, FileMode.OpenOrCreate, FileAccess.ReadWrite)

fos.Write(MesageFile, 0, MesageFile.Length)

fos.Close()

}

上传的文件格式不限。

string src=""

if(openFileDialog.showDialog==DialogResult.OK)

{

src=openFileDialog.filename.toString()

image.save("图片名",src)

}

具体放哪你看着办吧!


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11492787.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存