In my asp mvc 3 application, I have an action which allows the user to download a given file.
Here is the code :
public FilePathResult DownloadFile(string fileName)
{
try
{
string uploadsDocumentPath = System.Configuration.ConfigurationManager.AppSettings["uploadsDocumentPath"].ToString();
string ext = Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext); // henter info fra windows registry
if (regKey != null && regKey.GetValue("Content Type") != null)
{
mimeType = regKey.GetValue("Content Type").ToString();
}
return File(uploadsDocumentPath + fileName, mimeType, fileName);
}
catch (Exception)
{
throw;
}
}
I want to be able to allow only files with size less than 150MB to be downloaded. But I can't find how to calculate this type of file's size.
Any ideas ?
I guess this should work:
FileInfo file = new FileInfo(uploadsDocumentPath + fileName);
if(file.Length > 157286400)
{
// Return error here.
}
Related
An attempt was made to implement file downloads through the SpringBoot MVC structure. There is no error, it says it has run normally, but the download does not proceed.
All information about the file is entered correctly, and also the path and name of the file are entered correctly.
I'd like to know why the download doesn't proceed even though there's no error.
#RestController
public class Controller {
#PostMapping("/fileDownload")
public void fileDownload(#RequestBody BoardFileDTO dto,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
//File contains all stored paths, names, and extensions
Path fileNamePath = Paths.get(Directory + dto.getFile_save_name()).toAbsolutePath();
String filename = dto.getFile_save_name(); //The name of the saved file
String downname = dto.getFile_name(); //The name of the file to be saved
if (filename == null || "".equals(filename)) {
filename = downname;
}
try {
String browser = request.getHeader("User-Agent");
//File Encoding
if (browser.contains("MSIE") || browser.contains("Trident")
|| browser.contains("Chrome")) {
filename = URLEncoder.encode(filename, "UTF-8").replaceAll("\\+",
"%20");
} else {
filename = new String(filename.getBytes("UTF-8"), "ISO-8859-1");
}
} catch (UnsupportedEncodingException ex) {
System.out.println("UnsupportedEncodingException");
}
System.out.println(fileNamePath);
File file1 = new File(fileNamePath.toString());
if (!file1.exists()) {
return ;
}
// Specifying a File
response.setContentType("application/octer-stream");
response.setHeader("Content-Transfer-Encoding", "binary;");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
try {
OutputStream os = response.getOutputStream();
FileInputStream fis = new FileInputStream(fileNamePath.toString());
int ncount = 0;
byte[] bytes = new byte[512];
while ((ncount = fis.read(bytes)) != -1 ) {
os.write(bytes, 0, ncount);
}
fis.close();
os.close();
} catch (FileNotFoundException ex) {
System.out.println("FileNotFoundException");
} catch (IOException ex) {
System.out.println("IOException");
}
}
}
Your code is a bit convoluted imho. A couple of issues I see with your code
Using Path.toString to convert to a File, use the proper factory methods instead or use java.nio.Files to check the existence.
Your content-type is wrong application/octer-stream isn't a known content-type (you probably want application/octet-stream.
Copying from a Path or File is better done with either the StreamUtils from Spring or the java.nio.Files class (if you already have a Path use that).
#PostMapping("/fileDownload")
public void fileDownload(#RequestBody BoardFileDTO dto, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
//File contains all stored paths, names, and extensions
Path fileNamePath = Paths.get(Directory, dto.getFile_save_name()).toAbsolutePath();
if (!Files.exists(fileNamePath)) {
return;
}
String filename = determineFilename(dto, request);
// Specifying a File
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader("Content-Transfer-Encoding", "binary;");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
try {
Files.copy(fileNamePath, response.getOutputStream());
} catch (IOException ex) {
System.out.println("IOException");
}
}
private static String determineFilename(BoardFileDTO dto, HttpServletRequest request) {
String filename = dto.getFile_save_name(); //The name of the saved file
if (filename == null || "".equals(filename)) {
filename = dto.getFile_name();
}
String browser = request.getHeader("User-Agent");
//File Encoding
if (browser.contains("MSIE") || browser.contains("Trident") || browser.contains("Chrome")) {
filename = URLEncoder.encode(filename, StandardCharsets.UTF_8).replaceAll("\\+", "%20");
} else {
filename = new String(filename.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
}
return filename;
}
It would write it something like that. As you have a path use the java.nio.Files to check for existence and copying. Use constants for mediatypes and charsets.
Your error handling is quite basic (I would say non-existing and at least not proper) as the processing just stops and returns an empty 200 to the client. No information what so ever.
I took the liberty to factor out the logic to determine the filename, which should make your code more readable.
I keep getting file or directory does not exist. I am running within a Groovy script that creates a Spring Application Context. I am easily reading in a different file using the same type of pathing. However, the file I am reading is in the class path of Spring. This script might be run by any number of people with different file systems, so I can't hard code a path. I need a relative path.
This is above in the class but important info.
private static String saveFilesToLocation = "/retrieve/";
Here is the code.
CSVReader reader = new CSVReader(new InputStreamReader(balanceFile), SEPARATOR)
String[] nextLine
int counter = 0;
while ((nextLine = reader.readNext()) != null) {
counter++
if (nextLine != null && (nextLine[0] != 'FileLocation') ) {
counter++;
try {
//Remove 0, only if client number start with "0".
String fileLocation = nextLine[0];
byte[] fileBytes = documentFileService.getFile(fileLocation);
if (fileBytes != null) {
String fileName = fileLocation.substring(fileLocation.indexOf("/") + 1, fileLocation.length());
File file = new File(saveFilesToLocation+fileLocation);
file.withOutputStream {
it.write fileBytes
}
println "$counter) Wrote file ${fileLocation} to ${saveFilesToLocation+fileLocation}"
} else {
println "$counter) UNABLE TO RETRIEVE FILE: $fileLocation";
}
} catch (Exception e) {
e.printStackTrace()
}
}
}
The paths in Strings have what I would expect in them, no extra characters.
UPDATE:
Thanks loteq Your answer would work too, and has better grooviness than our final result that worked. Since it is a sort of one off, we don't have the time to change to the nicer version you have.
Here is the code that worked for us, it is identical to above except the saveFilesToLocation is set to a directory that already exists now. The one before didn't exist and we would have needed to call mkdir like loteq
suggested.
private static String saveFilesToLocation = "/tmp/retrieve/";
CSVReader reader = new CSVReader(new InputStreamReader(balanceFile), SEPARATOR)
String[] nextLine
int counter = 0;
while ((nextLine = reader.readNext()) != null) {
if (nextLine != null && (nextLine[0] != 'FileLocation') ) {
counter++;
try {
//Remove 0, only if client number start with "0".
String fileLocation = nextLine[0];
byte[] fileBytes = documentFileService.getFile(fileLocation);
if (fileBytes != null) {
String fileName = fileLocation.substring(fileLocation.indexOf("/") + 1, fileLocation.length());
File file = new File(saveFilesToLocation+fileName);
file.withOutputStream {
it.write fileBytes
}
println "$counter) Wrote file ${fileLocation} to ${saveFilesToLocation+fileLocation}"
} else {
println "$counter) UNABLE TO RETRIEVE FILE: $fileLocation";
}
} catch (Exception e) {
e.printStackTrace()
}
} else {
counter++;
}
}
There seems to be something add in your code, but I can't be certain that it's a bug.
You compute a fileName and don't really use it to create the target file. Instead you just append the original path to the prefix saveFilesToLocation:
String fileName = fileLocation.substring(fileLocation.indexOf("/") + 1, fileLocation.length());
File file = new File(saveFilesToLocation+fileLocation);
This seems strange.
Then, if fileLocation contains directories that need to be created, then you need to mkdirs() them, otherwise you will get an error.
I will give you 2 snippets, one atht assumes that youir code above is buggy, te other that does what you do in a safer way, in idiomatic groovy.
First lets work with actual File objects instead if Strings:
private static File saveFilesToLocationDir = saveFilesToLocation as File
Version that supposes a bug in the above code:
private static String saveFilesToLocation = "/retrieve/";
private static File saveFilesToLocationDir = saveFilesToLocation as File
CSVReader reader = new CSVReader(new InputStreamReader(balanceFile), SEPARATOR)
String[] nextLine
int counter = 0;
while ((nextLine = reader.readNext()) != null) {
counter++
if (nextLine != null && (nextLine[0] != 'FileLocation')) {
counter++;
try {
//Remove 0, only if client number start with "0".
String fileLocation = nextLine[0];
byte[] fileBytes = documentFileService.getFile(fileLocation);
if (fileBytes != null) {
int firstSlash = fileLocation.indexOf("/") + 1
String fileName = fileLocation[firstSlash..-1]
File destination = new File(saveFilesToLocationDir, fileName)
destination.parentFile.mkdirs()
destination.withOutputStream { it << fileBytes }
println "$counter) Wrote file ${fileLocation} to ${destination.absolutePath}"
} else {
println "$counter) UNABLE TO RETRIEVE FILE: $fileLocation";
}
} catch (Exception e) {
e.printStackTrace()
}
}
}
Version that does not use the created fileName (like you):
private static String saveFilesToLocation = "/retrieve/";
private static File saveFilesToLocationDir = saveFilesToLocation as File
CSVReader reader = new CSVReader(new InputStreamReader(balanceFile), SEPARATOR)
String[] nextLine
int counter = 0;
while ((nextLine = reader.readNext()) != null) {
counter++
if (nextLine != null && (nextLine[0] != 'FileLocation')) {
counter++;
try {
//Remove 0, only if client number start with "0".
String fileLocation = nextLine[0];
byte[] fileBytes = documentFileService.getFile(fileLocation);
if (fileBytes != null) {
int firstSlash = fileLocation.indexOf("/") + 1
String fileName = fileLocation[firstSlash..-1]
File destination = new File(saveFilesToLocationDir, fileLocation)
destination.parentFile.mkdirs()
destination.withOutputStream { it << fileBytes }
println "$counter) Wrote file ${fileLocation} to ${destination.absolutePath}"
} else {
println "$counter) UNABLE TO RETRIEVE FILE: $fileLocation";
}
} catch (Exception e) {
e.printStackTrace()
}
}
}
I upload a file in my MVC 3 project.
[HttpPost]
public ActionResult MyUpload(HttpPostedFileBase file)
{
string filePath = string.Empty;
string path = "C:\\";
string filePath = string.Empty;
try
{
if (file != null && file.ContentLength > 0)
{
filePath = path + file.FileName;
file.SaveAs(filePath);
file.InputStream.Dispose();
GC.Collect();
// other operations, where can occur an exception
// (because the uploaded file can have a bad content etc.)
}
}
catch (Exception e)
{
if (file.InputStream != null)
file.InputStream.Dispose();
GC.Collect();
if (!string.IsNullOrEmpty(filePath))
{
if (System.IO.File.Exists(filePath))
System.IO.File.Delete(filePath); //here is the error
}
}
}
in that code, if an exception occured after I saved the file I can't delete it (also I cann't upload it again) because I get the error
The process cannot access the file
'[filePath]' because it is being used by another process.
What's wrong with that code ?
edit
I had to change the file.InputStream.Dispose(); to
file.InputStream.Close();
file.InputStream.Dispose();
file.InputStream = null;
And, now it's working fine.
Instead of checking if the file.InputStream is not null inside the catch block, you should dispose it inside the finally block, like this:
if (file != null && file.ContentLength > 0)
{
try
{
filePath = path + file.FileName;
file.SaveAs(filePath);
// other operations, where can occur an exception
// (because the uploaded file can have a bad content etc.)
}
catch (Exception e)
{
if (!string.IsNullOrEmpty(filePath))
{
if (System.IO.File.Exists(filePath))
System.IO.File.Delete(filePath); //here is the error
}
}
finally
{
file.InputStream.Close();
file.InputStream.Dispose();
GC.Collect();
}
}
By the way, the InputStream property is a read-only property. You can't set it to null.
I use C#-MVC3. I have an "export" page. I have some functions for exporting different tables from the DB, every function creates a CSV file from the table and returns a FileContentResult file to the user.
Now I want to create a button for "export all", to download all the files at once.
I tried to use ZipFile but it gets only file names and path - files that were saved on the server, not "FileContentResult" files.
So I wanted to save the "FileContentResult" files temporarily on the server, zip them and delete them - but I can't find how to save a "FileContentResult" file.
If you can help me or give me another idea, I'll glad to hear.
my solution:
public ZipFile DownloadAllToZip()
{
string path = "c:\\TempCSV";
try
{
if (Directory.Exists(path))
{
EmptyFolder(path);
}
else
{
DirectoryInfo di = Directory.CreateDirectory(path);
}
List<FileContentResult> filesToExport = GetAllCSVs();
foreach (var file in filesToExport)
{
try
{
using (FileStream stream = new FileStream(path + "\\" + file.FileDownloadName, FileMode.CreateNew))
{
using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
{
byte[] buffer = file.FileContents;
stream.Write(buffer, 0, buffer.Length);
writer.Close();
}
}
}
catch { }
}
}
catch{ }
Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=MaterialAssetTracker.zip");
ZipFile zip= new ZipFile();
using (zip)
{
zip.CompressionLevel = CompressionLevel.None;
zip.AddSelectedFiles("*.csv", path + "\\", "", false);
zip.Save(Response.OutputStream);
}
Response.Close();
EmptyFolder(path);
return zip;
}
Im not sure what the exact term should i called. I want to add shortcut to my C# program when i right click in windows.
From my findings, it got something to do with configure the "regedit". I have this example, but it was made for IE. can anyone point me to any references that can solve my problems?
references:
http://blog.voidnish.com/?p=17
http://www.codeguru.com/cpp/misc/misc/internetexplorer/article.php/c11007/
thank you very much.
UPDATED today..
Based on response from Factor Mystic, i add this code to the original. I have 2 solutions. One, It was created in registry HKEY_ CLASSES_ ROOT, but i cannot see the result when i right click the doc files.
private const string ProgName = "Software\\Classes\\Word.Document\\shell";
private const string MenuName = "Software\\Classes\\Word.Document\\shell\\NewTesting";
public const string Command =Software\\Classes\\Word.Document\\shell\\NewTesting\\command";
private void Form1_Load(object sender, EventArgs e)
{
txtProgram.Text = "Word.Document.8";
txtName.Text = "Testing";
txtPath.Text = "C:\\temp\\encriptTest.exe";
check();
addItem()
}
public void check()
{
RegistryKey regmenu = null;
RegistryKey regcmd = null;
try
{
//this.CheckSecurity();
regmenu = Registry.ClassesRoot.OpenSubKey(MenuName, false);
}
catch (ArgumentException ex)
{
// RegistryPermissionAccess.AllAccess can not be used as a parameter for GetPathList.
MessageBox.Show(this, "An ArgumentException occured as a result of using AllAccess. "
+ "AllAccess cannot be used as a parameter in GetPathList because it represents more than one "
+ "type of registry variable access : \n" + ex);
}
catch (SecurityException ex)
{
// RegistryPermissionAccess.AllAccess can not be used as a parameter for GetPathList.
MessageBox.Show(this, "An ArgumentException occured as a result of using AllAccess. " + ex);
this.btnAddMenu.Enabled = false;
//this.btnRemoveMenu.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString());
}
finally
{
if (regmenu != null)
regmenu.Close();
if (regcmd != null)
regcmd.Close();
}
}
private void CheckSecurity()
{
//check registry permissions
RegistryPermission regPerm;
regPerm = new RegistryPermission(RegistryPermissionAccess.Write, "HKEY_CLASSES_ROOT\\" + ProgName);
regPerm.AddPathList(RegistryPermissionAccess.Write, "HKEY_CLASSES_ROOT\\" + MenuName);
regPerm.AddPathList(RegistryPermissionAccess.Write, "HKEY_CLASSES_ROOT\\" + Command);
regPerm.Demand();
}
private void addItem()
{
RegistryKey regmenu = null;
RegistryKey regcmd = null;
RegistryKey regprog = null;
try
{
regprog = Registry.ClassesRoot.CreateSubKey(ProgName);
if (regmenu != null)
regmenu.SetValue("", this.txtProgram.Text);
regmenu = Registry.ClassesRoot.CreateSubKey(MenuName);
if (regmenu != null)
regmenu.SetValue("", this.txtName.Text);
regcmd = Registry.ClassesRoot.CreateSubKey(Command);
if (regcmd != null)
regcmd.SetValue("", this.txtPath.Text);
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString());
}
finally
{
if (regprog != null)
regprog.Close();
if (regmenu != null)
regmenu.Close();
if (regcmd != null)
regcmd.Close();
}
}
Second, create in HKEY_ LOCAL_ MACHINE.
private bool Add_Item(string Extension,string MenuName, string MenuDescription, string MenuCommand)
{
//receive .doc,OpenTest,Open with Opentest,path: C:\\temp\\encriptTest.exe %1
bool ret = false;
RegistryKey rkey = //receive .doc extension (word.Document.8)
Registry.ClassesRoot.OpenSubKey(Extension); //set HKEY_LOCAL_MACHINE\software\classes\word.Document.8
if (rkey != null)
{
string extstring = rkey.GetValue("").ToString();
rkey.Close();
if (extstring != null)
{
if (extstring.Length > 0)
{
rkey = Registry.ClassesRoot.OpenSubKey(extstring, true);
if (rkey != null) //with extension file receive OpenTest as shell
{
string strkey = "shell\\" + MenuName + "\\command"; //..\shell\OpenTest\command
RegistryKey subky = rkey.CreateSubKey(strkey);
if (subky != null)
{
subky.SetValue("", MenuCommand); // path: C:\\temp\\encriptTest.exe %1
subky.Close();
subky = rkey.OpenSubKey("shell\\" + MenuName, true); //..\shell\OpenTest
if (subky != null)
{
subky.SetValue("", MenuDescription); // name displayed: Open with &OpenTest
subky.Close();
}
ret = true;
}
rkey.Close();
}
}
}
}
return ret;
}
}
My concerned, which Main Key should i use?
I believe you want to add items to the Explorer context menu.
Here is a nice article on CodeProject that shows you how to do it:
http://www.codeproject.com/KB/cs/appendmenu.aspx (basically it's just adding the appropriate keys to the windows registry)
You're going to want to determine the file type (ProgID) of .doc files. You can find this in HKEY_CURRENT_USER\Software\Classes\.doc (it is the default value).
Then add the key HKEY_CURRENT_USER\Software\Classes\<ProgID>\shell\NewMenuOption\command, where the default value is the path to your program.
You can do all this with Registry.SetValue and GetValue.
Check out this msdn page to get started.
Edit: Additional info, the difference between hive keys:
HKEY_LOCAL_MACHINE\Software\Classes and HKEY_CURRENT_USER\Software\Classes are similar, but HKLM is for system defaults/all user settings, and HKCU is for per user settings. Per user settings don't require elevated privileges, so you can write your context menu keys as a regular user with no pain.
HKEY_CLASSES_ROOT is a view combining HKEY_LOCAL_MACHINE\Software\Classes and HKEY_CURRENT_USER\Software\Classes, with writes directed to HKLM. This is a shortcut to writing system default values, and many tutorials show this because it's slightly simpler, but unless you're installing the application for all users I don't recommend it.
Advanced registry info on MSDN
Thank you very much for the responses. Very2 apreciate Them..
As Per Conlcusion, 3 ways on solving my prob. in easy understadable approach:
Adding shortcut in 3 ways:
1. create directly in registry window:
http://www.codeguru.com/cpp/misc/misc/internetexplorer/article.php/c11007/
2. shortcut available only to folders.
http://www.codeproject.com/KB/cs/appendmenu.aspx
http://blog.voidnish.com/?p=17
3. shortcut available to all files and folders.
http://www.autoitscript.com/forum/index.php?showtopic=103265&view=findpost&p=731920