This is the code from where result come
HttpFileCollection files = Request.Files;
for (int i = 0; i < files.Count; i++) {
HttpPostedFile file = files[i];
string fileExtension = GetFileExtension(file.FileName);
if (IsImageFile(fileExtension)) {
bool dbSuccessful = false;
string statusMessage = "Error inserting image into database";
string targetFilePath = WebsiteCommon.GetVersion4OriginalProductImageFilePath(file.FileName);
if (!File.Exists(targetFilePath)) {
file.SaveAs(targetFilePath);
}
Product.Image images = WebsiteCommon.Product();
FileViewModel imageFile = null;
images.LoadImage(file.FileName);
string urlFormat = @ "\v4\{0}\images\product_originals\{1}";
if (images.HasRows()) {
imageFile = new FileViewModel() {
ID = images.ImageID, Path = String.Format(urlFormat, WebsiteCommon.ShopID.ToString(), images.OriginalFileName), OriginalFileName = images.OriginalFileName
};
statusMessage = String.Format("'{0}' has already been uploaded.", file.FileName);
dbSuccessful = true;
images.Unload();
} else {
images.Unload();
System.Drawing.Image img = System.Drawing.Image.FromFile(targetFilePath);
string fileNameMinusExtension = file.FileName.Substring(0, file.FileName.LastIndexOf("."));
dbSuccessful = images.InsertImage(Guid.Empty, Guid.Empty, Product.Image.ImageGroup_OriginalProductImages, "", 0, "." + fileExtension,
file.FileName, img.Width, img.Height, null, fileNameMinusExtension);
statusMessage = String.Format("'{0}' uploaded successfully.", file.FileName);
images.LoadImage(file.FileName);
imageFile = new FileViewModel() {
ID = images.ImageID, Path = String.Format(urlFormat, WebsiteCommon.ShopID.ToString(), images.OriginalFileName), OriginalFileName = images.OriginalFileName
};
images.Unload();
}
string productCode = GetProductCode(file.FileName);
Product.ProductDetail product = WebsiteCommon.InitialiseProductDetails();
classes.Responses.ProductSummaryResponse productMatch = null;
if (product.LoadProductDetailsFromCode(productCode, WebsiteCommon.CatalogueID, true, true)) {
productMatch = new classes.Responses.ProductSummaryResponse() {
Code = productCode, IsSuccessful = true, ProductDetailID = product.ProductDetailID.ToString(), ProductID = Guid.Empty.ToString(), Title = product.Title, ProductImages = new List < FileViewModel > () {
imageFile
}
};
} else {
productMatch = new classes.Responses.ProductSummaryResponse() {
Code = "", IsSuccessful = false, ProductDetailID = Guid.Empty.ToString(), ProductID = Guid.Empty.ToString(), Title = "", ProductImages = new List < FileViewModel > () {
imageFile
}
};
}
UploadFileResponse response = new UploadFileResponse {
FileName = file.FileName,
StatusMessage = statusMessage,
Statuses = null,
IsSuccessful = dbSuccessful,
ProductMatchedByCode = productMatch
};
Response.Write(JsonConvert.SerializeObject(response, Formatting.None));
} else {
Response.Write("ERROR: Cant upload this type of file: " + file.FileName);
}
}
WebsiteCommon.WebServices_Close();
Response.End();
}
}
private string GetProductCode(string fileName) {
string name = fileName.IndexOf('.') > 0 ? fileName.Substring(0, fileName.LastIndexOf('.')) : fileName;
if (name.Length > 4 && char.IsDigit(name[name.Length - 1]) && name.Contains("_")) {
int index = name.Length - 1;
while (index > 0 && char.IsDigit(name[index])) {
index--;
}
if (index > 0 && name[index] == '_') {
index--;
while (index > 0 && char.IsDigit(name[index])) {
index--;
}
if (index > 0 && name[index] == '-') {
// remove char before size
name = name.Substring(0, index);
}
}
}
if (name.Length > 2 && char.IsDigit(name[name.Length - 1]) && name.Contains("_")) {
int index = name.Length - 1;
while (index > 0 && char.IsDigit(name[index])) {
index--;
}
if (index > 0 && name[index] == '_') {
name = name.Substring(0, index);
}
}
return name;
}
}
}
if (images.HasRows()) {
imageFile = new FileViewModel() {
ID = images.ImageID, Path = String.Format(urlFormat, WebsiteCommon.ShopID.ToString(), images.OriginalFileName), OriginalFileName = images.OriginalFileName
};
statusMessage = String.Format("'{0}' has already been uploaded.", file.FileName);
dbSuccessful = true;
images.Unload();
} else {
images.Unload();
System.Drawing.Image img = System.Drawing.Image.FromFile(targetFilePath);
string fileNameMinusExtension = file.FileName.Substring(0, file.FileName.LastIndexOf("."));
dbSuccessful = images.InsertImage(Guid.Empty, Guid.Empty, Product.Image.ImageGroup_OriginalProductImages, "", 0, "." + fileExtension,
file.FileName, img.Width, img.Height, null, fileNameMinusExtension);
statusMessage = String.Format("'{0}' uploaded successfully.", file.FileName);
images.LoadImage(file.FileName);
imageFile = new FileViewModel() {
ID = images.ImageID, Path = String.Format(urlFormat, WebsiteCommon.ShopID.ToString(), images.OriginalFileName), OriginalFileName = images.OriginalFileName
};
images.Unload();
}
string productCode = GetProductCode(file.FileName);
Product.ProductDetail product = WebsiteCommon.InitialiseProductDetails();
classes.Responses.ProductSummaryResponse productMatch = null;
if (product.LoadProductDetailsFromCode(productCode, WebsiteCommon.CatalogueID, true, true)) {
productMatch = new classes.Responses.ProductSummaryResponse() {
Code = productCode, IsSuccessful = true, ProductDetailID = product.ProductDetailID.ToString(), ProductID = Guid.Empty.ToString(), Title = product.Title, ProductImages = new List < FileViewModel > () {
imageFile
}
};
} else {
productMatch = new classes.Responses.ProductSummaryResponse() {
Code = "", IsSuccessful = false, ProductDetailID = Guid.Empty.ToString(), ProductID = Guid.Empty.ToString(), Title = "", ProductImages = new List < FileViewModel > () {
imageFile
}
};
}
UploadFileResponse response = new UploadFileResponse {
FileName = file.FileName,
StatusMessage = statusMessage,
Statuses = null,
IsSuccessful = dbSuccessful,
ProductMatchedByCode = productMatch
};
Response.Write(JsonConvert.SerializeObject(response, Formatting.None));
}
else {
Response.Write("ERROR: Cant upload this type of file: " + file.FileName);
}
}
WebsiteCommon.WebServices_Close();
Response.End();
}
}
private string GetProductCode(string fileName) {
string name = fileName.IndexOf('.') > 0 ? fileName.Substring(0, fileName.LastIndexOf('.')) : fileName;
if (name.Length > 4 && char.IsDigit(name[name.Length - 1]) && name.Contains("_")) {
int index = name.Length - 1;
while (index > 0 && char.IsDigit(name[index])) {
index--;
}
if (index > 0 && name[index] == '_') {
index--;
while (index > 0 && char.IsDigit(name[index])) {
index--;
}
if (index > 0 && name[index] == '-') {
// remove char before size
name = name.Substring(0, index);
}
}
}
if (name.Length > 2 && char.IsDigit(name[name.Length - 1]) && name.Contains("_")) {
int index = name.Length - 1;
while (index > 0 && char.IsDigit(name[index])) {
index--;
}
if (index > 0 && name[index] == '_') {
name = name.Substring(0, index);
}
}
return name;
}
}
}