Kentico 11 - MediaFileInfoProvider.DeleteMediaFileInfo Not Deleting - kentico

MediaFileInfo updateFile = MediaFileInfoProvider.GetMediaFileInfo(library.LibraryID, file.Name);
The above line of code is not removing the file from the media library as I expected. This is for a Scheduled Task in Kentico 11 MVC. The new file does get created and is renamed by the System to prevent conflicts.
I would like to delete the existing file before importing the updated version of the file. I would even be satisfied if the new file overwrote the existing file.
public void UpdateMediaFile(MediaLibraryInfo library, string fileName, string importPath)
{
//LumberMarketReport.pdf and PanelMarketReport.pdf
if (library != null)
{
// Prepares a path to a local file
string filePath = fileName;
// Prepares a CMS.IO.FileInfo object representing the local file
CMS.IO.FileInfo file = CMS.IO.FileInfo.New(filePath);
if (file != null)
{
#region "Delete Existing"
MediaFileInfo updateFile = MediaFileInfoProvider.GetMediaFileInfo(library.LibraryID, file.Name);
if (updateFile != null)
{
MediaFileInfoProvider.DeleteMediaFileInfo(updateFile);
}
#endregion
#region "Create File"
// Creates a new media library file object
MediaFileInfo mediaFile = new MediaFileInfo(filePath, library.LibraryID);
// Sets the media library file properties
mediaFile.FileName = file.Name;
mediaFile.FileDescription = "This file was added through the API.";
mediaFile.FilePath = "/"; // Sets the path within the media library's folder structure
mediaFile.FileExtension = file.Extension;
mediaFile.FileMimeType = MimeTypeHelper.GetMimetype(file.Extension);
mediaFile.FileSiteID = SiteContext.CurrentSiteID;
mediaFile.FileLibraryID = library.LibraryID;
mediaFile.FileSize = file.Length;
if (file.Name == "PanelMarketReport.pdf")
{
mediaFile.FileTitle = "Panel Market Report";
mediaFile.SetValue("FileCategoryID", 19);
}
else if (file.Name == "LumberMarketReport.pdf")
{
mediaFile.FileTitle = "Lumber Market Report";
mediaFile.SetValue("FileCategoryID", 57);
}
// Saves the media library file
MediaFileInfoProvider.SetMediaFileInfo(mediaFile);
#endregion
}
}
}

I would add logging to make sure your code is getting hit. Make sure updatefile isn't null. I think you have to pass in the file path in the media library and not just the name.

If I am not mistaken MediaFileInfoProvider.DeleteMediaFileInfo will remove a record from DB but not physically delete the file, so you need to call CMS.IO.FileInfo.Delete(filePath) to delete it from disk.

Related

Input output stream not working in Web Forms function

Can someone tell me why I keep getting a read and write timeout on this function? I have this as a code behind function on click even from a button. Everything as far as the data looks good until I get to the stream section and it still steps through, but when I check the Stream object contents after stepping into that object it states Read Timeout/Write Timeout: System invalid Operation Exception.
protected void SubmitToDB_Click(object sender, EventArgs e)
{
if (FileUploader.HasFile)
{
try
{
if (SectionDropDownList.SelectedValue != null)
{
if (TemplateDropDownList.SelectedValue != null)
{
// This gets the full file path on the client's machine ie: c:\test\myfile.txt
string strFilePath = FileUploader.PostedFile.FileName;
//use the System.IO Path.GetFileName method to get specifics about the file without needing to parse the path as a string
string strFileName = Path.GetFileName(strFilePath);
Int32 intFileSize = FileUploader.PostedFile.ContentLength;
string strContentType = FileUploader.PostedFile.ContentType;
//Convert the uploaded file to a byte stream to save to your database. This could be a database table field of type Image in SQL Server
Stream strmStream = FileUploader.PostedFile.InputStream;
Int32 intFileLength = (Int32)strmStream.Length;
byte[] bytUpfile = new byte[intFileLength + 1];
strmStream.Read(bytUpfile, 0, intFileLength);
strmStream.Close();
saveFileToDb(strFileName, intFileSize, strContentType, bytUpfile); // or use FileUploader.SaveAs(Server.MapPath(".") + "filename") to save to the server's filesystem.
lblUploadResult.Text = "Upload Success. File was uploaded and saved to the database.";
}
}
}
catch (Exception err)
{
lblUploadResult.Text = "The file was not updloaded because the following error happened: " + err.ToString();
}
}
else
{
lblUploadResult.Text = "No File Uploaded because none was selected.";
}
}
Try something like this:
using (var fileStream = FileUploader.PostedFile.InputStream)
{
using (var reader = new BinaryReader(fileStream))
{
byte[] bytUpfile = reader.ReadBytes((Int32)fileStream.Length);
// SAVE TO DB...
}
}

Uable to open exported excel file

I am creating an asp.net Core API with an endpoint to export data as excel file.
I have added the nuget package EPPlus.Core.
This is my code so far:
using OfficeOpenXml;
...
Public class DataService
{
...
public Byte[] Export(ExportRequest request){
var data = _repository(request);
using (var package = new ExcelPackage())
{
package.Workbook.Properties.Title = "Persons";
var worksheet = package.Workbook.Worksheets.Add("Persons");
//Headers
worksheet.Cells[1, 1].Value = "Id";
worksheet.Cells[1, 2].Value = "Name";
//Values
var row = 2;
foreach (var person in data.Persons)
{
worksheet.Cells[row, 1].Value = person.Id;
worksheet.Cells[row, 2].Value = person.Name;
row++;
}
return package.GetAsByteArray();
}
}
public class PersonController : Controller
{
[HttpPost]
[Route("{version:apiVersion}/[controller]/Export")]
[SwaggerResponse(400, typeof(Exception), "Error in request")]
[SwaggerResponse(500, typeof(Exception), "Error on server")]
public IActionResult Export([FromBody] ExportRequest request)
{
Byte[] byteArray = _projectService.ExportRegistrations(request);
if (byteArray != null)
{
return File(byteArray, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "persons.xlsx");
}
return BadRequest("Failure");
}
}
I test my API using swagger and get a file downloaded named persons.xlsx.
But I am not able to open it.
I get the two following promts:
We found a problem in some of the contents of "persons.xlsx", should we try to restore as much as possible? If you trust the source of the workbook, click Yes.
The file "persons.xlsx" can not be opened because the file format or file type name is invalid. Verify that the file is not corrupted and that the file type name matches the file format.
And then an empty excel document.
i have also facing the same problem,
The workaround is directly open the url via browser or using postman.

Mvc 5 return file mimetype does not work

What I want to achieve is returning file from my specified folder with the correct file type and specified the name. My action returns the file as it should but content type does not set. I want it to be specified as dynamically from my document path. On debug mode, I can see the file's extension set to the contentType argument.
public async Task<FileResult> Download(int? id, string refId, int? categoryId = null)
{
var document = await _documentService.GetDocument( Id = id.Value );
if (document == null || document.RefId.ToString() != refId)
{
throw new Exception("NotFound");
}
var directory = await _categoryService.GetCategory(Session.UserId.Value, categoryId);
var archive = Server.MapPath(directory + "/" + document.FileName);
var contentType = MimeMapping.GetMimeMapping(document.FileName);
return File(archive, contentType, document.PureFileName);
}
Thanks in advance.
I have added file extension to the end of document.PureFileName argument and now it's working as expected.

Downloading bulk files from sharepoint library

I want to download the files from a sharepoint document library through code as there are thousand of files in the document library.
I am thinking of creating console application, which I will run on sharepoint server and download files. Is this approach correct or, there is some other efficient way to do this.
Any help with code will be highly appreciated.
Like SigarDave said, it's perfectly possible to achieve this without writing a single line of code. But if you really want to code the solution for this, it's something like:
static void Main(string[] args)
{
// Change to the URL of your site
using (var site = new SPSite("http://MySite"))
using (var web = site.OpenWeb())
{
var list = web.Lists["MyDocumentLibrary"]; // Get the library
foreach (SPListItem item in list.Items)
{
if (item.File != null)
{
// Concat strings to get the absolute URL
// to pass to an WebClient object.
var fileUrl = string.Format("{0}/{1}", site.Url, item.File.Url);
var result = DownloadFile(fileUrl, "C:\\FilesFromMyLibrary\\", item.File.Name);
Console.WriteLine(result ? "Downloaded \"{0}\"" : "Error on \"{0}\"", item.File.Name);
}
}
}
Console.ReadKey();
}
private static bool DownloadFile(string url, string dest, string fileName)
{
var client = new WebClient();
// Change the credentials to the user that has the necessary permissions on the
// library
client.Credentials = new NetworkCredential("Username", "Password", "Domain");
var bytes = client.DownloadData(url);
try
{
using (var file = File.Create(dest + fileName))
{
file.Write(bytes, 0, bytes.Length); // Write file to disk
return true;
}
}
catch (Exception)
{
return false;
}
}
another way without using any scripts is by opening the document library using IE then in the ribbon you can click on Open in File Explorer where you can then drag and drop the files right on your desktop!

How to zip and unzip folders and its sub folders in Silverlight?

I have a Windows Phone application. I am using SharpZipLib to zip folders and its sub folders. This is zipping only the folder but the data inside the folders is not getting zipped. Can anyone guide me how to do this?
My code:
private void btnZip_Click(object sender, RoutedEventArgs e)
{
using (IsolatedStorageFile appStore = IsolatedStorageFile.GetUserStoreForApplication())
{
foreach (string filename in appStore.GetFileNames(directoryName + "/" + "*.txt"))
{
GetCompressedByteArray(filename);
}
textBlock2.Text = "Created file has Zipped Successfully";
}
}
public byte[] GetCompressedByteArray(string content)
{
byte[] compressedResult;
using (MemoryStream zippedMemoryStream = new MemoryStream())
{
using (ZipOutputStream zipOutputStream = new ZipOutputStream(zippedMemoryStream))
{
zipOutputStream.SetLevel(9);
byte[] buffer;
using (MemoryStream file = new MemoryStream(Encoding.UTF8.GetBytes(content)))
{
buffer = new byte[file.Length];
file.Read(buffer, 0, buffer.Length);
}
ZipEntry entry = new ZipEntry(content);
zipOutputStream.PutNextEntry(entry);
zipOutputStream.Write(buffer, 0, buffer.Length);
zipOutputStream.Finish();
}
compressedResult = zippedMemoryStream.ToArray();
}
WriteToIsolatedStorage(compressedResult);
return compressedResult;
}
public void WriteToIsolatedStorage(byte[] compressedBytes)
{
IsolatedStorageFile appStore = IsolatedStorageFile.GetUserStoreForApplication();
appStore.CreateDirectory(ZipFolder);
using (IsolatedStorageFileStream zipTemplateStream = new IsolatedStorageFileStream(ZipFolder+"/"+directoryName + ".zip", FileMode.OpenOrCreate, appStore))
using (BinaryWriter streamWriter = new BinaryWriter(zipTemplateStream))
{
streamWriter.Write(compressedBytes);
}
}
I think you'll find this guide helpful.
An excerpt from the above link
The ZipFile object provides a method called AddDirectory() that
accepts a parameter directoryName. The problem with this method is
that it doesn't add the files inside the specified directory but
instead just creates a directory inside the zip file. To make this
work, you need to get the files inside that directory by looping thru
all objects in that directory and adding them one at a time. I was
able to accomplish this task by creating a recursive function that
drills through the whole directory structure of the folder you want to
zip. Below is a snippet of the function.
I guess you too are facing the same problem where the folder is added to the zip file, but the contents and sub folders are not zipped.
Hope this helps.
Have a look over here for a code sample on how to use SharpZipLib to zip a root folder including nested folders.

Resources