-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileUploadServ.svc.cs
108 lines (87 loc) · 4.38 KB
/
FileUploadServ.svc.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Web.Hosting;
using System.Data.Entity;
using System.Web;
namespace WcfAlfa
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "FileUploadServ" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select FileUploadServ.svc or FileUploadServ.svc.cs at the Solution Explorer and start debugging.
public class FileUploadServ : WsBase, IFileUploadServ
{
public Stream UpdateFile(string id, string uuid, string codigo,long version)
{
try
{
alfadbEntities db = new alfadbEntities();
int LibroId = Int16.Parse(id);
//codigos Libro = db.codigos.Where(c => c.Codigo == codigo && c.UUID == uuid && ).Single()
versiones Libro = db.versiones.Where(i => i.LibroId == LibroId && i.Version == version).SingleOrDefault();
//string downloadFilePath = Path.Combine(HostingEnvironment.MapPath("~/FileServer/Extracts"), fileName + "." + fileExtension);
string downloadFilePath = Path.Combine("C:\\Descargar_Libros\\updates\\", Libro.NombreArchivo + ".zip");
// open stream
System.IO.FileStream stream = new System.IO.FileStream(downloadFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
String headerInfo = "attachment; filename=" + Libro.NombreArchivo + ".zip";
WebOperationContext.Current.OutgoingResponse.Headers["Content-Disposition"] = headerInfo;
HttpContext.Current.Response.Headers.Add("Content-Length", stream.Length.ToString());
WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
return stream;
}
catch (Exception ex)
{
return null;
}
}
public Stream DownloadFile(string id, string uuid, string codigo)
{
try
{
alfadbEntities db = new alfadbEntities();
int LibroId = Int16.Parse(id);
//codigos Libro = db.codigos.Where(c => c.Codigo == codigo && c.UUID == uuid && ).Single()
libroscodigos Libro = db.libroscodigos.Include(i => i.codigos).Include(i => i.libros).Where(c => c.codigos.Codigo == codigo && c.codigos.UUID == uuid && c.libros.Id == LibroId).SingleOrDefault();
//string downloadFilePath = Path.Combine(HostingEnvironment.MapPath("~/FileServer/Extracts"), fileName + "." + fileExtension);
string downloadFilePath = Path.Combine("C:\\Descargar_Libros\\", Libro.libros.Nombre + ".zip");
// open stream
System.IO.FileStream stream = new System.IO.FileStream(downloadFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
String headerInfo = "attachment; filename=" + Libro.libros.Nombre + ".zip";
WebOperationContext.Current.OutgoingResponse.Headers["Content-Disposition"] = headerInfo;
HttpContext.Current.Response.Headers.Add("Content-Length", stream.Length.ToString());
WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
return stream;
}
catch (Exception ex)
{
return null;
}
}
public void UploadFile(string fileName, Stream stream)
{
try
{
string FilePath = Path.Combine(HostingEnvironment.MapPath("~/FileServer/Uploads"), fileName);
int length = 0;
using (FileStream writer = new FileStream(FilePath, FileMode.Create))
{
int readCount;
var buffer = new byte[8192];
while ((readCount = stream.Read(buffer, 0, buffer.Length)) != 0)
{
writer.Write(buffer, 0, readCount);
length += readCount;
}
}
}
catch (Exception ex)
{
Error(ex, "El Libro");
}
}
}
}