Пример #1
0
        public static byte[] GetImage(string dir, string id, bool toByteArray)
        {
            //if (!toByteArray) GetImage(dir, id);
            var container = BlobStorageService.GetCloudBlobContainer();
            var blob      = container.GetDirectoryReference(dir).GetDirectoryReference(id).GetBlockBlobReference(id);

            if (blob.Exists())
            {
                blob.FetchAttributes();
                var image = new byte[blob.Properties.Length];
                blob.DownloadToByteArray(image, 0);
                return(image);
            }
            var defaultImg = container.GetDirectoryReference(dir).GetDirectoryReference("Default").GetBlockBlobReference("Default.jpg");

            if (defaultImg.Exists())
            {
                defaultImg.FetchAttributes();
                var image = new byte[defaultImg.Properties.Length];
                defaultImg.DownloadToByteArray(image, 0);
                return(image);
            }

            return(null);
        }
        public ActionResult DownLoads(string dir, string name)
        {
            dir = dir.Replace("-", "/");
            BlobStorageService svBlob = new BlobStorageService();
            var container             = svBlob.GetCloudBlobContainer();

            Response.AddHeader("Content-Disposition", "attachment; filename=" + name); // force download

            container.GetBlockBlobReference(dir + "/" + name).DownloadToStream(Response.OutputStream);
            return(new EmptyResult());
        }
Пример #3
0
        public string ImageURL(HttpPostedFileBase imageUpload)
        {
            CloudBlobContainer blobContainer = BlobStorageService.GetCloudBlobContainer();
            //Khai báo Blob với tên hình được upload
            CloudBlockBlob blob = blobContainer.GetBlockBlobReference(imageUpload.FileName);

            //Lưu trữ hình vào blob
            blob.UploadFromStream(imageUpload.InputStream);
            //Lấy đường dẫn Blob
            var bUri = blob.Uri.AbsoluteUri;

            return(bUri);
        }
Пример #4
0
        public ActionResult SetImage(HttpPostedFileBase fileBase, string dir, string id)
        {
            if (fileBase.ContentLength <= 0)
            {
                return(RedirectToAction("Index", "UserAdmin"));
            }
            var container = BlobStorageService.GetCloudBlobContainer();
            var path      = dir + "/" + id;
            var directory = container.GetDirectoryReference(path);

            directory.GetBlockBlobReference(id).DeleteIfExists();
            var blob = directory.GetBlockBlobReference(id);

            blob.UploadFromStream(fileBase.InputStream);

            return(View("Index", "UserAdmin"));
        }
Пример #5
0
        public string ImageURL(HttpPostedFileBase imageUpload)
        {
            //Nếu có tập tin được upload
            if (imageUpload != null)
            {
                //Kiểm tra có phải là hình ảnh không
                if (imageUpload.ContentType.Contains("image"))
                {
                    //Kiểm tra số byte của hình được upload
                    if (imageUpload.ContentLength > 0)
                    {
                        //Kiểm sa nếu ảnh lớn hơn 4mb
                        if (imageUpload.ContentLength > 4 * 1048576)
                        {
                            return("File upload(" + ((double)imageUpload.ContentLength / 1048576).ToString("0.00") + ") is not larger than 4MB.");
                        }
                        else
                        {
                            CloudBlobContainer blobContainer = BlobStorageService.GetCloudBlobContainer();
                            //Khai báo Blob với tên hình được upload
                            CloudBlockBlob blob = blobContainer.GetBlockBlobReference(imageUpload.FileName);
                            //Lưu trữ hình vào blob
                            blob.UploadFromStream(imageUpload.InputStream);
                            //Lấy đường dẫn Blob
                            var bUri = blob.Uri.AbsoluteUri;
                            return(bUri);
                        }
                    }
                }
                else
                {
                    return("File upload must be image.");
                }
            }
            else
            {
                return("No file is selected");
            }

            return(null);
        }
Пример #6
0
        public static string GetImage(string dir, string id)
        {
            var container = BlobStorageService.GetCloudBlobContainer();
            var directory = container.GetDirectoryReference(dir).GetDirectoryReference(id);
            var blob      = directory.GetBlockBlobReference(id);
            var image     = "";

            if (blob.Exists())
            {
                image = blob.Uri.ToString();
            }
            if (image != "")
            {
                return(image);
            }
            var defaultImg = container.GetDirectoryReference(dir).GetDirectoryReference("Default").GetBlockBlobReference("Default.jpg");

            if (defaultImg.Exists())
            {
                image = defaultImg.Uri.ToString();
            }

            return(image);
        }