-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilePro.cs
651 lines (538 loc) · 22.8 KB
/
FilePro.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
using System;
using System.Globalization;
using System.Linq;
using System.Net.Mail;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Configuration;
using System.Windows.Forms;
using iTextSharp.text.pdf.parser;
namespace SiginBS
{
public class FileUploadInfo
{
public HttpPostedFile File { get; set; }
public string FileName { get; set; }
public int Chunk { get; set; }
public int TotalChunk { get; set; }
public static FileUploadInfo FromRequest(HttpRequest request)
{
if (request.Files == null || request.Files.Count == 0)
{
return null;
}
return new FileUploadInfo
{
File = request.Files[0],
FileName = request.Headers["name"],
Chunk = Convert.ToInt32(request.Headers["chunk"]),
TotalChunk = Convert.ToInt32(request.Headers["chunks"])
};
}
}
public enum ResultCode : int
{
#region Common error codes (000 ~ 099)
NoError = 1,
UnknownError,
TokenInvalid,
NotFoundResourceId,
IdNotMatch,
NotModified,
WaitNextRequest,
DataInvalid = 8,
DataIsUsed = 9,
FileLarge = 10,
#endregion Common error codes
#region System error codes (100 ~ 199)
SystemConfigNotFound = 100,
SystemConfigInvalid,
FileNotFound,
ReadWriteFileError,
#endregion System error codes
#region Client error codes (200 ~ 199)
RequestDataInvalid = 200,
NotEnoughPermission,
#endregion Client error codes
#region Noitice Invoice error codes (300 ~ 399)
// Login, Session error codes
UserIsDisabled = 300,
UserNotFound,
LoginFailed,
SessionAlive,
SessionEnded,
// Insert, update data error codes
ConflictResourceId,
#endregion Noitice Invoice error codes
#region Common error codes (1000 ~ 1999)
NotAuthorized = 1000,
NotPermisionData,
TimeOut,
#endregion User operation error codes
#region Login error codes (2000 ~ 2009)
LoginUserIdIsEmpty = 2000,
LoginUserIdNotExist,
LoginEmailInvalid,
LoginEmailNotExist,
LoginPasswordIsEmpty,
LoginOldPasswordIncorrect,
#endregion
#region Invoice error codes (2010 ~ 2019)
InvoiceReleasedNotDelete = 2010,
InvoiceReleasedNotUpdate = 2011,
NumberInvoiceOverloadRegister = 2012,
InvoiceIsConverted = 2013,
InvoiceDateInvalid = 2014,
#endregion
#region User Account Management error codes (2020 ~ 2039)
UserAccountMgtUsernameExceedMaxLength = 2020,
UserAccountMgtUsernameIsEmpty,
UserAccountMgtEmailExceedMaxLength,
UserAccountMgtPasswordExceedMaxLength,
UserAccountMgtEmailIsEmpty,
UserAccountMgtEmailInvalid,
UserAccountMgtPasswordIsEmpty,
UserAccountMgtConflictResourceUserId = 2027,
UserAccountMgtUserIdIsEmpty = 2028,
UserAccountMgtUserIdExceedMaxLength,
UserAccountMgtConflictResourceEmail,
UserAccountMgtCompanyIdNotFound = 2031,
UserAccountMgtNotPermissionDelete,
UserAccountMgtConflictResourceTaxCode = 2033,
#endregion Project management error codes
#region Client error codes (2040~2059)
// English name exceeds maximum length
ClientEnglishNameExceedsMaxLength = 2040,
// Local name exceeds maximum length
ClientLocaNameExceedsMaxLength,
// English address exceeds maximum length
ClientEnglishAddressExceedsMaxLength,
// Local address exceeds maximum length
ClientLocalAddressExceedsMaxLength,
// Phone number exceeds maximum length
ClientPhoneNumberExceedsMaxLength,
// Fax number exceeds maximum length
ClientFaxNumberExceedsMaxLength,
// Contact person's name exceeds maximum length
ClientContactNameExceedsMaxLength,
// Email address exceeds maximum length
ClientContactEmailExceedsMaxLength,
// You have to enter at least a name of client
ClientClientNameIsEmpty,
// You have to enter at least a address of client
ClientAddressIsEmpty,
// Format of email is invalid
ClientEmailInvalidFormat,
#endregion
#region Country admin account management error codes(2080 ~ 2999)
CountryAdminMgtSearchTextExceedMaxLength = 2080,
CountryAdminMgtUsernameExceedMaxLength,
CountryAdminMgtFormatUserNameInvalid,
CountryAdminMgtEmailExceedMaxLength,
CountryAdminMgtEmailInvalid,
CountryAdminMgtPasswordInvalid,
CountryAdminMgtPasswordExceedMaxLength,
CountryAdminMgtEmailIsEmpty,
CountryAdminMgtPasswordIsEmpty,
#endregion Country admin account management error codes
#region Company error Code (3000 ~ 3029)
CompanyNameBlank = 3000,
CompanyAddressBlank,
CompanyAdminNameBlank,
CompanyAdminEmailBlank,
CompanyTaxInvalid,
CompanyEnglishCompanyNameMaxLength,
CompanyLocalCompanyNameMaxLength,
CompanyEnglishAddressMaxLength,
CompanyLocalAddressMaxLength,
CompanyFirstPhoneNumberMaxLength,
CompanyPhoneNumberMaxLength = 3010,
CompanyFaxNumberMaxLength = 3011,
CompanyHomepageMaxLength,
CompanyAdminNameMaxLength,
CompanyAdminEmailMaxLength,
CompanyAdminEmailInvalid,
CompanyNameMaxLength = 3016,
CompanyNameIsExitsTaxCode = 3017,
CompanyNameIsExitsEmail = 3018,
CompanyNameCannotCreated = 3019,
#endregion
#region Register Template error Code (3030 ~ 3060)
TemplateInvoiceBeingused = 3030,
TemplateCodeIdIsExisted = 3031,
#endregion
#region Noitice Invoice error codes (4000 ~ 4050)
NoiticeUseInvoiceApprovedNotUpdate = 4000,
NoiticeUseInvoiceApprovedNotDelete = 4001,
NoticeDetailSymbolIsExisted = 4002,
InvoiceApprovedNotHasCustomer = 4003,
InvoiceApprovedNotHasItem = 4004,
#endregion Noitice Invoice error codes
#region Release Invoice error codes (4051 ~ 4100)
ReleaseInvoiceApprovedNotUpdate = 4051,
ReleaseUseInvoiceApprovedNotDelete = 4052,
ReleaseDetailSymbolIsExisted = 4053,
#endregion Noitice Invoice error codes
#region Cancelling Invoice error codes (4101 ~ 4140)
InvoiceIssuedNotUpdate = 4101,
#endregion Cancelling Invoice error codes
#region ImportData
ImportDataSizeOfFileTooLarge = 5050,
ImportFileFormatInvalid = 5051,
ImportColumnIsNotExist,
ImportDataIsEmpty = 5053,
ImportDataExceedMaxLength,
ImportDataFormatInvalid,
ImportDataNotSuccess,
ImportDataIsExisted = 5057,
ImportDataIsNotNumberic = 5058,
ImportDataIsNotDateTime,
ImportDataDaeteOfInvoiceInvalid = 5060,
#endregion
#region PayMent
ExceedAmountOfInvoice = 6000,
#endregion
#region Contract
ContractNotYetApporved = 8000,
ContractNoExisted,
ContractNotExisted,
ContractCustomerNotYetUse,
ContractLoginUserIsEmpty = 8004,
ContractDownloaded = 8005,
ContractCustomerUsedInvoice,
#endregion
#region Agencies
AgenciesHasContract = 9000,
#endregion
#region Customer
CustomerHasContract = 9050,
#endregion
}
public class UploadFileCommon
{
public UploadFileCommon() { }
//public ResultCode SaveFile(FileUploadInfo fileUploadInfo, string filePath)
//{
// var file = fileUploadInfo.File;
// if (file == null || file.InputStream == null || string.IsNullOrWhiteSpace(filePath))
// {
// return ResultCode.RequestDataInvalid;
// }
// ResultCode resultCode = ResultCode.WaitNextRequest;
// using (var fs = new FileStream(filePath, fileUploadInfo.Chunk == 0 ? FileMode.Create : FileMode.Append))
// {
// var buffer = new byte[Config.ApplicationSetting.Instance.MaxLengthBuffer];
// int bytesRead;
// while ((bytesRead = file.InputStream.Read(buffer, 0, buffer.Length)) > 0)
// {
// fs.Write(buffer, 0, bytesRead);
// }
// }
// if (fileUploadInfo.Chunk == (fileUploadInfo.TotalChunk - 1))
// {
// resultCode = ResultCode.NoError;
// }
// return resultCode;
//}
//How to create multiple directories from a single full path in C#?
public static List<String> GetAllFiles(String directory)
{
if (!directory.EndsWith("\\"))
{
directory = directory + "\\";
}
return Directory.GetFiles(directory, "*", SearchOption.AllDirectories).ToList();
}
public string CreateMultiplePath(String path)
{// Lay thu muc LocalLow
// string currentForlder = Path.Combine(Environment.GetFolderPathEnvironment.SpecialFolder.ApplicationData), "template");
//C:\Users\ccanpv\AppData\LocalLow\xmlvb
// string folderPath = HttpContext.Current.Server.MapPath(path);
if(!path.EndsWith("\\"))
{
path = path + "\\";
}
String folderPath = path;
try
{
string folder = System.IO.Path.GetDirectoryName(folderPath);
if (!Directory.Exists(folder))
{
try
{
// Try to create the directory.
DirectoryInfo di = Directory.CreateDirectory(folder);
}
catch { }
}
}
catch (IOException ioex)
{
// Console.WriteLine(ioex.Message);
return ioex.Message;
}
return path;
}
public bool CreateFilePathByDocId(string path)
{
// string path = Path.Combine(Config.ApplicationSetting.Instance.FolderAssetOfCompany, companyId.ToString(), AssetSignInvoice.Release, AssetSignInvoice.TempSignFile, releaseId.ToString());
string folderPath = HttpContext.Current.Server.MapPath(path);
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
return true;
// return Path.Combine(folderPath, fileUploadInfo.FileName);
}
public bool IsUploadedFile(FileUploadInfo fileUploadInfo)
{
return fileUploadInfo.Chunk == (fileUploadInfo.TotalChunk - 1);
}
//public string StandardFileUpload(string sourceFile)
//{
// string newFilePath = string.Empty;
// try
// {
// FileInfo fileInfo = new FileInfo(sourceFile);
// var extension = fileInfo.Extension;
// var nameWithoutExtension = fileInfo.Name.Remove(fileInfo.Name.LastIndexOf(extension));
// string suffix = DateTime.Now.ToString(Formatter.DateTimeFormat);
// string newName = nameWithoutExtension + Characters.Underscore + suffix + extension;
// string folderPath = fileInfo.DirectoryName;
// string destFile = Path.Combine(folderPath, newName);
// fileInfo.MoveTo(destFile);
// int companyId = GetCompanyIdOfUser();
// string path = Path.Combine(Config.ApplicationSetting.Instance.FolderAssetOfCompany, companyId.ToString(), AssetSignInvoice.Release, AssetSignInvoice.SignFile);
// string tagetFullPath = HttpContext.Current.Server.MapPath(path);
// if (!Directory.Exists(tagetFullPath))
// {
// Directory.CreateDirectory(tagetFullPath);
// }
// FileProcess.ExtractFile(destFile, tagetFullPath);
// newFilePath = destFile;
// DeleteFolderTempl(folderPath);
// }
// catch
// {
// // Don't need handle exception.
// // When exception, will set file path is source file
// // TODO: Maybe, write log to trace exception.
// newFilePath = string.Empty;
// }
// return newFilePath;
//}
//private void DeleteFolderTempl(string destFile)
//{
// if (destFile.IsNotNullOrEmpty() && Directory.Exists(destFile))
// {
// Directory.Delete(destFile, true);
// }
//}
//public string StandardFileUpload(string sourceFile, int companyId)
//{
// string newFilePath = string.Empty;
// try
// {
// FileInfo fileInfo = new FileInfo(sourceFile);
// var extension = fileInfo.Extension;
// var nameWithoutExtension = fileInfo.Name.Remove(fileInfo.Name.LastIndexOf(extension));
// string suffix = DateTime.Now.ToString(Formatter.DateTimeFormat);
// string newName = nameWithoutExtension + Characters.Underscore + suffix + extension;
// string folderPath = fileInfo.DirectoryName;
// string destFile = Path.Combine(folderPath, newName);
// fileInfo.MoveTo(destFile);
// string path = Path.Combine(Config.ApplicationSetting.Instance.FolderAssetOfCompany, companyId.ToString(), AssetSignInvoice.Release, AssetSignInvoice.SignFile);
// string tagetFullPath = HttpContext.Current.Server.MapPath(path);
// if (!Directory.Exists(tagetFullPath))
// {
// Directory.CreateDirectory(tagetFullPath);
// }
// FileProcess.ExtractFile(destFile, tagetFullPath);
// newFilePath = destFile;
// DeleteFolderTempl(folderPath);
// }
// catch
// {
// // Don't need handle exception.
// // When exception, will set file path is source file
// // TODO: Maybe, write log to trace exception.
// newFilePath = string.Empty;
// }
// return newFilePath;
//}
}
public class SendMail
{
public SendMail()
{
}
// public static async Task<bool> _send(string subject, string body, string[] MailTo)
//{
// string MailFrom = System.Configuration.ConfigurationManager.AppSettings["MailFrom"];
// string MailFromPassword = System.Configuration.ConfigurationManager.AppSettings["MailFromPassword"];
// //string MailTo = System.Configuration.ConfigurationManager.AppSettings["MailTo"];
// // string MailTo2 = System.Configuration.ConfigurationManager.AppSettings["MailTo2"];
// //string MailTo3 = System.Configuration.ConfigurationManager.AppSettings["MailTo3"];
// System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
// foreach (string s in MailTo)
// {
// var foo = new EmailAddressAttribute();
// bool ebar;
// ebar = foo.IsValid(s);
// if (ebar) mail.To.Add(s);// to mailfo
// //mail.To.Add(MailTo2);// to mail
// }// mail.To.Add(MailTo3);// to mail
// mail.From = new MailAddress(MailFrom, "Your Health");
// mail.Subject = subject;
// //DateTime dateLogin = DateTime.Now;
// mail.Body = body;
// //mail.Body = "Bạn đã đăng nhập thành công với tài khoản: " + "<div><B> "+ model.Username + "</B></div>" + " vào lúc " + DateTime.Now.ToString("hh:mm tt dd/MM/yyyy");
// mail.IsBodyHtml = true;
// SmtpClient smtp = new SmtpClient();
// smtp.Host = "smtp.gmail.com";
// smtp.Port = 587;
// smtp.UseDefaultCredentials = true;
// smtp.Credentials = new System.Net.NetworkCredential(MailFrom, MailFromPassword);
// smtp.EnableSsl = true;
// try
// {
// await smtp.SendMailAsync(mail);
// // tam thoi khong gui mail
// return true;
// }
// catch
// {
// // return false;
// }
// return false;
//}
}
//public class eMail
//{
// public void SendEmailWebMail(string subject, string body,
// string toAddress,
// string cc = null, string bcc = null,
// IEnumerable<string> attachmentFilePath = null,
// IDictionary<string, string> headers = null)
// {
// System.Web.Mail.MailMessage message = new System.Web.Mail.MailMessage();
// //from, to, reply to
// // message.From = new MailAddress(fromAddress, fromName);
// string fromAddress = ConfigurationManager.AppSettings["fromAddress"];
// message.From = fromAddress;
// //message.From = new MailAddress("[email protected]", "[email protected]");
// //message.To.Add(new MailAddress(toAddress, toName));
// message.To = toAddress; //"[email protected]";
// // message.Cc = cc;
// //if (!String.IsNullOrEmpty(replyTo))
// //{
// // message.ReplyToList = (new MailAddress(replyTo, replyToName));
// //}
// //BCC
// if (bcc != null)
// {
// //foreach (var address in bcc.Where(bccValue => !String.IsNullOrWhiteSpace(bccValue)))
// //{
// message.Bcc = bcc;//(address.Trim());
// //}
// }
// //message.Bcc = "[email protected]";
// //CC
// if (cc != null)
// {
// // foreach (var address in cc.Where(ccValue => !String.IsNullOrWhiteSpace(ccValue)))
// // {
// message.Cc = cc;//(address.Trim());
// //}
// }
// message.Subject = subject;
// message.Body = body;
// message.BodyEncoding = System.Text.Encoding.UTF8;
// message.BodyFormat = MailFormat.Html;
// //LinkedResource logo = new LinkedResource("");
// //System.Net.Mail.Attachment attachment;
// //attachment = New System.Net.Mail.Attachment(Server.MapPath("~/App_Data/hello.pdf"));
// //mail.Attachments.Add(attachment);
// if (attachmentFilePath != null)
// {
// foreach (var strFile in attachmentFilePath.Where(attachmentFilePathValue => !String.IsNullOrWhiteSpace(attachmentFilePathValue)))
// {
// MailAttachment attachFile = new MailAttachment(strFile);
// message.Attachments.Add(attachFile);
// }
// // message.Attachments.GetType().
// //message.Attachments.Add(attachFile);
// }
// string smptServer = ConfigurationManager.AppSettings["smptServer"];
// string emailAccount = ConfigurationManager.AppSettings["emailAccount"];
// string emailPass = ConfigurationManager.AppSettings["emailPass"];
// string emailPort = ConfigurationManager.AppSettings["emailPort"];
// string smtpusessl = ConfigurationManager.AppSettings["smtpusessl"];
// string smtpconnectiontimeout = System.Configuration.ConfigurationManager.AppSettings["smtpconnectiontimeout"];
// SmtpMail.SmtpServer = smptServer;//
// message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
// message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", emailAccount);
// message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", emailPass);
// message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", emailPort);
// message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", smtpusessl);
// message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout", smtpconnectiontimeout);
// SmtpMail.Send(message);
// // Log log = new Log(); log.Logwrite(DateTime.Now.ToString("yyyyMMdd"), message.ToString());
// }
// /// <summary>
// /// Chuyen file anh về dang bytes
// /// </summary>
// /// <param name="path"></param>
// /// <returns></returns>
// public byte[] ImageBytes(string path)
// {
// byte[] imageBytes = null;
// Image a = new Bitmap(path);//Bitmap(@".../path/to/image.png");
// using (MemoryStream ms = new MemoryStream())
// {
// // Convert Image to byte[]
// a.Save(ms, a.RawFormat);
// imageBytes = ms.ToArray();
// //this.richTextBox1.Text = "<img src=\"data:image/png;base64," + Convert.ToBase64String( imageBytes ) + "\"/>";
// }
// return imageBytes;
// }
//}
public class Footer
{
public string Name { get; set; } = string.Empty;
public string Chucvu { get; set; } = string.Empty;
public string Phong { get; set; } = string.Empty;
public string Mobile { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
}
public static class fileCommon
{
//picturebox1.Image = fileCommon.LoadBitmap("LOCATION");
public static Bitmap LoadBitmap(string path)
{
if (File.Exists(path))
{
// open file in read only mode
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
// get a binary reader for the file stream
using (BinaryReader reader = new BinaryReader(stream))
{
// copy the content of the file into a memory stream
var memoryStream = new MemoryStream(reader.ReadBytes((int)stream.Length));
// make a new Bitmap object the owner of the MemoryStream
return new Bitmap(memoryStream);
}
}
else
{
MessageBox.Show("Error Loading File.", "Error!", MessageBoxButtons.OK);
return null;
}
}
}
}