当前位置: 首页>>代码示例>>C#>>正文


C# MultipartFormDataStreamProvider.DeleteLocalFiles方法代码示例

本文整理汇总了C#中System.Net.Http.MultipartFormDataStreamProvider.DeleteLocalFiles方法的典型用法代码示例。如果您正苦于以下问题:C# MultipartFormDataStreamProvider.DeleteLocalFiles方法的具体用法?C# MultipartFormDataStreamProvider.DeleteLocalFiles怎么用?C# MultipartFormDataStreamProvider.DeleteLocalFiles使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Http.MultipartFormDataStreamProvider的用法示例。


在下文中一共展示了MultipartFormDataStreamProvider.DeleteLocalFiles方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ProductImages

		public async Task<IQueryable<UploadImage>> ProductImages()
		{
			if (!Request.Content.IsMimeMultipartContent())
			{
				throw this.ExceptionUnsupportedMediaType();
			}

			Product entity = null;
			string identifier = null;
			string tempDir = FileSystemHelper.TempDir();
			var provider = new MultipartFormDataStreamProvider(tempDir);

			try
			{
				await Request.Content.ReadAsMultipartAsync(provider);
			}
			catch (Exception exception)
			{
				provider.DeleteLocalFiles();
				throw this.ExceptionInternalServerError(exception);
			}

			// find product entity
			if (provider.FormData.AllKeys.Contains("Id"))
			{
				identifier = provider.FormData.GetValues("Id").FirstOrDefault();
				entity = _productService.Value.GetProductById(identifier.ToInt());
			}
			else if (provider.FormData.AllKeys.Contains("Sku"))
			{
				identifier = provider.FormData.GetValues("Sku").FirstOrDefault();
				entity = _productService.Value.GetProductBySku(identifier);
			}
			else if (provider.FormData.AllKeys.Contains("Gtin"))
			{
				identifier = provider.FormData.GetValues("Gtin").FirstOrDefault();
				entity = _productService.Value.GetProductByGtin(identifier);
			}

			if (entity == null)
			{
				provider.DeleteLocalFiles();
				throw this.ExceptionNotFound(WebApiGlobal.Error.EntityNotFound.FormatInvariant(identifier.NaIfEmpty()));
			}

			// process images
			var equalPictureId = 0;
			var displayOrder = 0;
			var result = new List<UploadImage>();
			var storeUrl = _storeContext.Value.CurrentStore.Url;
			var pictures = entity.ProductPictures.Select(x => x.Picture);

			if (entity.ProductPictures.Count > 0)
				displayOrder = entity.ProductPictures.Max(x => x.DisplayOrder);

			foreach (var file in provider.FileData)
			{
				var image = new UploadImage(file.Headers);

				if (image.FileName.IsEmpty())
					image.FileName = entity.Name;

				var pictureBinary = File.ReadAllBytes(file.LocalFileName);

				if (pictureBinary != null && pictureBinary.Length > 0)
				{
					pictureBinary = _pictureService.Value.ValidatePicture(pictureBinary);
					pictureBinary = _pictureService.Value.FindEqualPicture(pictureBinary, pictures, out equalPictureId);

					if (pictureBinary != null)
					{
						var seoName = _pictureService.Value.GetPictureSeName(Path.GetFileNameWithoutExtension(image.FileName));

						var newPicture = _pictureService.Value.InsertPicture(pictureBinary, image.MediaType, seoName, true, false, false);

						if (newPicture != null)
						{
							_productService.Value.InsertProductPicture(new ProductPicture
							{
								PictureId = newPicture.Id,
								ProductId = entity.Id,
								DisplayOrder = ++displayOrder
							});

							image.Inserted = true;
							image.Picture = newPicture;
						}
					}
					else
					{
						image.Exists = true;
						image.Picture = pictures.FirstOrDefault(x => x.Id == equalPictureId);
					}

					if (image.Picture != null)
					{
						image.ImageUrl = _pictureService.Value.GetPictureUrl(image.Picture, _mediaSettings.Value.ProductDetailsPictureSize, false, storeUrl);
						image.ThumbImageUrl = _pictureService.Value.GetPictureUrl(image.Picture, _mediaSettings.Value.ProductThumbPictureSize, false, storeUrl);
						image.FullSizeImageUrl = _pictureService.Value.GetPictureUrl(image.Picture, 0, false, storeUrl);
					}
//.........这里部分代码省略.........
开发者ID:toannguyen241994,项目名称:SmartStoreNET,代码行数:101,代码来源:UploadsController.cs


注:本文中的System.Net.Http.MultipartFormDataStreamProvider.DeleteLocalFiles方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。