Exemplo n.º 1
0
        public static ViewDataUploadFileResult StoreFile(Action<MvcFileSave> action)
        {
            var mvcFile = new MvcFileSave();
            mvcFile.FileTimeStamp = DateTime.Now.ToUniversalTime();
            action(mvcFile);
            
            ViewDataUploadFileResult status;
            
            var dirInfo = new DirectoryInfo(mvcFile.StorageDirectory);
            var file = mvcFile.File;
            var fileNameWithoutPath = Path.GetFileName(mvcFile.File.FileName);
            var fileExtension = Path.GetExtension(fileNameWithoutPath);
            var fileName = Path.GetFileNameWithoutExtension(Path.GetFileName(mvcFile.File.FileName));
            var genName = fileName + "-" + mvcFile.FileTimeStamp.ToFileTime();
            var genFileName = String.IsNullOrEmpty(mvcFile.FileName) ? genName + fileExtension : mvcFile.FileName;// get filename if set
            var fullPath = Path.Combine(mvcFile.StorageDirectory, genFileName);            

            try
            {                
                var viewDataUploadFileResult = new ViewDataUploadFileResult()
                {
                    name = fileNameWithoutPath,
                    SavedFileName = genFileName,
                    size = file.ContentLength,
                    type = file.ContentType,
                    url = mvcFile.UrlPrefix + "/" + genFileName,
                    //delete_url = Url.Action("DeleteFile", new { fileUrl = "/"+storageRoot+"/" + genFileName }),
                    //thumbnail_url = thumbUrl + "?width=100",
                    deleteType = "POST",
                    Title = fileName,

                    //for controller use
                    FullPath = dirInfo.FullName + "/" + genFileName
                };

                //add delete url                           
                mvcFile.AddFileUriParamToDeleteUrl("fileUrl", viewDataUploadFileResult.url);
                viewDataUploadFileResult.deleteUrl = mvcFile.DeleteUrl;
                

                status = viewDataUploadFileResult;   

                mvcFile.File.SaveAs(fullPath);
            }
            catch (Exception exc)
            {
                if (mvcFile.ThrowExceptions)
                    throw;
                
                status = new ViewDataUploadFileResult()
                             {
                                 error = exc.Message,
                                 name = file.FileName,
                                 size = file.ContentLength,
                                 type = file.ContentType 
                             };
            }

            return status;
        } 
Exemplo n.º 2
0
        public static ViewDataUploadFileResult StoreFile(Action<MvcFileSave> action)
        {
            var mvcFile = new MvcFileSave();
            action(mvcFile);

            ViewDataUploadFileResult status;

            var dirInfo = new DirectoryInfo(mvcFile.StorageDirectory);
            var file = mvcFile.File;
            var fileExtension = Path.GetExtension(mvcFile.File.FileName);
            var fileName = Path.GetFileNameWithoutExtension(mvcFile.File.FileName);
            var genName = fileName + "-" + DateTime.Now.ToFileTimeUtc();
            var genFileName = genName + fileExtension;
            var fullPath = Path.Combine(mvcFile.StorageDirectory, genFileName);

            try
            {
                mvcFile.File.SaveAs(fullPath);

                var viewDataUploadFileResult = new ViewDataUploadFileResult()
                {
                    name = file.FileName,
                    size = file.ContentLength,
                    type = file.ContentType,
                    url = mvcFile.UrlPrefix + "/" + genFileName,
                    //delete_url = Url.Action("DeleteFile", new { fileUrl = "/"+storageRoot+"/" + genFileName }),
                    //thumbnail_url = thumbUrl + "?width=100",
                    delete_type = "POST",
                    title = fileName,

                    //for controller use
                    fullpath = dirInfo.FullName + "/" + genFileName
                };

                //add delete url
                mvcFile.AddFileUriParamToDeleteUrl("fileUrl", viewDataUploadFileResult.url);
                viewDataUploadFileResult.delete_url = mvcFile.DeleteUrl;

                status = viewDataUploadFileResult;
            }
            catch (Exception exc)
            {
                status=new ViewDataUploadFileResult()
                {
                    error = "error: " + exc.Message,
                    name = file.FileName,
                    size = file.ContentLength,
                    type = file.ContentType,
                    title = fileName
                };
            }

            return status;
        }