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(basepath, "*"+ billNo + "*", SearchOption.TopDirectoryOnly);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.服务端查找要加载的图片(loadimages)

 1 public static object LoadImages(string billNo) {
 2             List<byte[]> headerBytePic = new List<byte[]>(); 4             string basepath = "D:\\image\\";
 5             if (!System.IO.Directory.Exists(basepath))
 6             {
 7                 return new { HeaderBytePic = headerBytePic, BehindBytePic = behindBytePic };
 8             }
 9             string[] strDataFiles = Directory.GetFiles(basepath, "*"+ billNo + "*", SearchOption.TopDirectoryOnly);
10 
11             //实验前
12             var headerPaths = from a in strDataFiles where a.Contains("实验前") && a.Contains(billNo) 
13                               && (a.EndsWith(".png") || a.EndsWith(".jpg")) select a;
14             foreach (string headerPath in headerPaths) {
15                 byte[] bytes = File.ReadAllBytes(headerPath);
16                 headerBytePic.Add(bytes);
17             }
26 return new {HeaderBytePic = headerBytePic}; 27 }

 

效果图

 

posted on 2021-08-11 09:49  写abcd的猴子  阅读(1143)  评论(0编辑  收藏  举报