WPF上传图片到服务器文件夹
1.前端用ListBox加载显示多张图片
1 <ListBox Name="lbHeadImages" Grid.Row="1" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
2 <ListBox.ItemTemplate>
3 <DataTemplate>
4 <Grid >
5 <Rectangle Fill="#fff">
6 <Rectangle.Effect>
7 <DropShadowEffect Opacity="0.5" ShadowDepth="0"/>
8 </Rectangle.Effect>
9 </Rectangle>
10 <StackPanel Margin="2">
11 <Image Stretch="UniformToFill" Width="100" Height="120" VerticalAlignment="Center" HorizontalAlignment="Center"
12 Source="{Binding Image,Mode=TwoWay,NotifyOnSourceUpdated=True}"
13 MouseLeftButtonDown="Image_MouseLeftButtonDown"></Image>
14 </StackPanel>
15 </Grid>
16 </DataTemplate>
17 </ListBox.ItemTemplate>
18 <ListBox.ItemsPanel>
19 <ItemsPanelTemplate>
20 <WrapPanel Name="wrapPanel" HorizontalAlignment="Stretch" />
21 <!--<UniformGrid Columns="6"></UniformGrid>-->
22 </ItemsPanelTemplate>
23 </ListBox.ItemsPanel>
24 </ListBox>
2.客户端——选择图片(可选择多张),前台加载显示
1 private void btnSelectBehindImage_Click(object sender, RoutedEventArgs e)
2 {
3 OpenFileDialog openFileDialog = new OpenFileDialog();
4 openFileDialog.Multiselect = true;
5 openFileDialog.Filter = "All Image Files|*.jpg;*.png";
6 if ((bool)openFileDialog.ShowDialog()) {
7 if (openFileDialog.OpenFile() != null) {
8 behindImages.Clear();
9 string[] fileNames = openFileDialog.FileNames;
10 List<FinishProRptCheckVM.ImageRow> items = new List<FinishProRptCheckVM.ImageRow>();
11 for (int i = 0; i < fileNames.Count(); i ++) {
12 string filePath = fileNames[i];
13 var size = new FileInfo(filePath).Length / 1024 /1024; //获取文件大小(M)
14 if (size >= 1) { MessageBox.Show("上传图片大小不能超过1M!");return; }
15 byte[] bytes = File.ReadAllBytes(filePath);
16 behindImages.Add(bytes);
17
18 FinishProRptCheckVM.ImageRow row = new FinishProRptCheckVM.ImageRow() { Image = new BitmapImage(new Uri(filePath))};
19 items.Add(row);
20 }
21 lbBehindImages.ItemsSource = items;
22 }
23 }
24 }
3.客户端上传图片
1 private bool UploadImage(string bill_no)
2 {
3 //上传服务器文件夹
4 string url = "/api/qc/iqc/experimentrptservice/uploadfile";
5 var result = Framework.PostData(url, lstBytes1, behindImages, bill_no);
6 if (result.success && result.data)
7 {
8 return true;
9 }
10 else
11 {
12 return false;
13 }
14 }
4.服务端保存图片到服务器文件夹
1 public static object UploadFile(List<byte[]> headerPhoto,List<byte[]> behindPhoto,string billNo) {
2 try
3 {
4 //获取保存文件的文件夹路径
5 string filePath = "D:\\image\\";
6 if (!System.IO.Directory.Exists(filePath))//文件夹不存在创建文件夹
7 {
8 Directory.CreateDirectory(filePath);
9 }
10 else {//文件夹存在,找对应实验是否存在上传过的图片,存在则删除
11 string[] strDataFiles = Directory.GetFiles(filePath);
12 var imageFiles = from a in strDataFiles where a.Contains(billNo) && (a.EndsWith(".png") || a.EndsWith(".jpg")) select a;
13 foreach (string imagePath in imageFiles) {
14 File.Delete(imagePath);
15 }
16 }
17 //保存实验前图片
18 for (int i = 0; i < headerPhoto.Count; i++)//遍历二进制的数组的数组
19 {
20 string strRiQiWenJian = "实验前" + i.ToString() + "_" + billNo + ".png";
21 string strBaoCunLuJing = "D:\\image\\" + strRiQiWenJian;
22 FileInfo fi = new FileInfo(strBaoCunLuJing);
23 FileStream fs;
24 fs = fi.OpenWrite();
25 fs.Write(headerPhoto[i], 0, headerPhoto[i].Length);
26 fs.Flush();
27 fs.Close();
28 }
29 return true;
30 }
31 catch
32 {
33 return false;
34 }
35 }
5.客户端,进入界面,初始化从服务器加载对应图片显示
1 public void LoadImages (){
2 string url = "/api/qc/iqc/experimentrptservice/loadimages?bill_no=" + ParamModel.BILL_NO;
3 var result = Framework.GetData(url);
4 if (result.success){
5 lstBytes1 = JsonConvert.DeserializeObject<List<byte[]>>(result.data.HeaderBytePic.ToString());
6 behindImages = JsonConvert.DeserializeObject<List<byte[]>>(result.data.BehindBytePic.ToString());
7
8 //加载实验前图片
9 List<FinishProRptCheckVM.ImageRow> headerImages = new List<FinishProRptCheckVM.ImageRow>();
10 for (int i = 0; i < lstBytes1.Count;i++) {
11 BitmapImage img = new BitmapImage();
12 img.BeginInit();
13 img.StreamSource = new MemoryStream(lstBytes1[i]);
14 img.EndInit();
15 //img.Freeze();
16 FinishProRptCheckVM.ImageRow row = new FinishProRptCheckVM.ImageRow() { Image = img };
17 headerImages.Add(row);
18 }
19 lbHeadImages.ItemsSource = headerImages;
20 }
21 }
6.效果图

WPF上传图片到服务器文件夹的更多相关文章
- 设置linux服务器文件夹权限
最近搞的网站一上传图片,就报500错误.经排查是服务器文件夹权限设置问题. 使用命令: chmod o+rwx avatar 即可改变文件夹权限设置.
- atitit. web 在线文件管理器最佳实践(1)--- elFinder 的使用流程解决之道 。打开浏览服务器文件夹java .net php
atitit. web 在线文件管理器最佳实践(1)--- elFinder 的使用流程解决之道 .打开浏览服务器文件夹java .net php 1. 环境:::项目java web,需要打开浏览服 ...
- 关于 百度 Ueditor 上传图片时 打开文件夹的延迟问题
在使用 ueditor 开发时, 作为一个web文本编辑器使用时. 当点击上传图片时, 文件夹要延迟好久才能打开. 解决: 针对多图片上传, 将/ueditor/dialogs/image/image ...
- java通过sftp对linux服务器文件夹进行操作
本文主要讲sftp对linux服务器的文件和文件夹进行操作,windows server 服务器不支持. package com.lx.ftp; import java.io.File; import ...
- 在MVC3中修改KindEditor实现上传图片到指定文件夹
KindEditor编辑器默认上传的图片文件夹,是根据系统时间自动生成的,图片是自动上传到这些文件夹里面,无法选择.如果要上传图片到指定文件夹,像相册一样管理图片,则需要扩展KindEditor编辑器 ...
- 百度编辑器 Ueditor 上传图片时打开文件夹的延迟问题,点击上传图片弹窗打开慢问题
在使用 ueditor 开发时, 作为一个web文本编辑器使用时. 当点击上传图片时, 文件夹要延迟好久才能打开. 解决: 针对多图片上传, 将/ueditor/dialogs/image/ima ...
- 关于 百度 Ueditor (在chrome浏览器) 上传图片时 打开文件夹的延迟问题
在使用 ueditor 开发时, 作为一个web文本编辑器使用时. 当点击上传图片时, 文件夹要延迟好久才能打开. 解决: 针对多图片上传, 将/ueditor/dialogs/image/image ...
- .net网站上传图片换电脑不显示 当不用网站的IP地址访问图片,只用相对路径访问时,在发布网站的时候,将上传图片的目标文件夹,包含在项目中再发布即可。
.net网站上传图片换电脑不显示 当不用网站的IP地址访问图片,只用相对路径访问时,在发布网站的时候,将上传图片的目标文件夹,包含在项目中再发布即可.
- WPF:自定义Metro样式文件夹选择对话框FolderBrowserDialog
1.前言 WPF并没有文件选择对话框,要用也就只有使用Winform版的控件.至今我也没有寻找到一个WPF版本的文件选择对话框. 可能是我眼浊,如果各位知道有功能比较健全的WPF版文件选择对话框.文件 ...
随机推荐
- ABP Framework:移除 EF Core Migrations 项目,统一数据上下文
原文:Unifying DbContexts for EF Core / Removing the EF Core Migrations Project 目录 导读:软件开发的一切都需要平衡 动机 警 ...
- 5.15、tomcat下部署JPress
1.说明: jpress类似于wordpress,wordpress是php语言开发的国外开源软件,jpress是java语言 开发的国内开源软件: 2.下载软件包: [root@slave-node ...
- nohup启动 jar 不输出日志
简单暴力:nohup java -jar xxx.jar >/dev/null 2>&1 &
- Linux文件编辑工具——VIM
Linux文件编辑工具--VIM 1.VIM基本概述 1.1 什么是vim vi 和 vim 是 Linux 下的一个文本编辑工具.(可以理解为 windows 的记事本,或 Notepad++ 1. ...
- imply套件以及plyql的安装
本文出自:http://www.cnblogs.com/gaojiang/p/7212149.html 1.安装nodejs,版本需要在4.0以上,安装方法见上篇博客 2.下载imply,地址:htt ...
- 在docker的镜像中安装vim
在使用docker容器时,有时候里边没有安装vim,敲vim命令时提示说:vim: command not found,这个时候就需要安装vim,可是当你敲apt-get install vim命令时 ...
- 《PHP设计模式大全》系列分享专栏
<PHP设计模式大全>已整理成PDF文档,点击可直接下载至本地查阅https://www.webfalse.com/read/201739.html 文章 php设计模式介绍之编程惯用法第 ...
- Spring Boot中的那些生命周期和其中的可扩展点(转)
前言可扩展点的种类Spring Boot启动过程 1.SpringApplication的启动过程 2.ApplicationContext的启动过程 3.一般的非懒加载单例Bean在Spring B ...
- linux驱动之LED驱动
通过之前的学习,了解到linux驱动编写的流程是:先通过注册函数注册我们编写的入口函数,然后在入口函数中获取设备号->注册字符设备->自动创建设备节点->获取设备树信息,最后通过销毁 ...
- 章节1-Grafana Dashboard的简单应用(2)
目录 使用Grafana创建可视化Dashboard 1. Add data sources - Prometheus 2. 导入 Dashboard 模板 2.1 Node Exporter for ...