Is it possible to get physical path of uploaded file using js/jquery? - filepath

I am trying for getting uploaded files actual physical path using JS or Jquery.
Currently I tried var tmppath = URL.createObjectURL(file);
Its proving temp path like
blob:http://fiddle.jshell.net/b298c63f-9501-4b09-b62e-c5eb01d4aaf3

Related

Save file to App_Data folder error

I am trying to save a pdf file generated using Rotativa to the app_data folder of my web app but I get the error :
System.IO.DirectoryNotFoundException: Could not find a part of the path 'E:\www\tsp13amp\website.com\wwwroot\App_Data\Documents\Corps_Profile_userID.pdf'.
My Controller code :
var PDF = new Rotativa.ViewAsPdf("ProfilePrint", model) { FileName = "Corps_Profile_" + User.Identity.GetUserId() + "_" + DateTime.Now.ToString("dd-MM-yyyy")};
var fileName = PDF.FileName;
byte[] pdfBytearray = PDF.BuildPdf(ControllerContext);
var fullPath = Path.Combine(Server.MapPath("~/App_Data/Documents/"),fileName +".pdf");
var byteArray = PDF.BuildPdf(ControllerContext);
System.IO.File.WriteAllBytes(fullPath, byteArray)
What I am trying to do is to store the file in the App_Data folder and a reference to the file in the database which will then be used a link to the file later on like:
Download
Thanks for any help.
Just to make this official - don't store anything in your app_data unless you specifically have a way it will be used. This folder is not mapped by default to return content, as such you'd need an HttpHandler or something specific to serve files from this folder. If you map them to a name in the database you'd still need a way to return them.
You are much better served creating another folder to use for this.

How to save the correct location of file in table? getting error is not a valid virtual path

I was trying to upload some images in my website using input type=file in my asp.net mvc project.while storing the image I used the following code and it is saving the image successfully.
var path2 = Path.Combine(Server.MapPath("~/Images"), filename.jpg);
artwork.SaveAs(path2);
After that I need to save the whole link ( http://example.com/Images/filename.jpg) into my table column.
for that i tried this
tablename.imagelink = path2 ;
then in table column I am getting like
D:\xxxxx\yyyy\example\Images\filename.jpg
then i tried to save the whole link directly
tablename.imagelink = Path.Combine(Server.MapPath("http://example.com/Images/"), filename.jpg);
at that time I am getting an error: "is not a valid virtual path error"
How to solve this. ?
Server.MapPath requires the virtual path of the web server, and you get the error because you're passing the full path like this:
Server.MapPath("http://example.com/Images/")
You need to keep using this:
Server.MapPath("~/Images")

asp.net image jpeg not saving correctly

I am trying to save a jpeg image in an uploads folder which has correct permissions setup. When I test the file is being saved (eg: images/uploads/Winter.jpg) but if I try to view the image in my browser or if I attempt to open the image using anything else the image does not display.
I think that the file is not being encoded correctly before saving it to disk but am not very experienced dealing with the saving of files, encoding. Does the below code look ok or do I need to encode the file being uploaded somehow before saving it to disk?
String imgPath = "newsletter\\images\\uploads\\";
String filename = System.IO.Path.GetFileName(upload.PostedFile.FileName);
filepath = imgPath + filename;
filepath = Request.PhysicalApplicationPath + filepath;
upload.PostedFile.SaveAs(filepath);
The file saves to the correct folder but is only 150bytes in size. If I try to browse to the file and view it with an image viewer it does not display correctly.
Encoding shouldn't be a problem - the raw data isn't changing. However, it's possible the browser isn't sending all the data, or that the upload control is deleting the data before you're saving it.
Make sure that you call .SaveAs() before the page begins unloading, and before any additional postbacks. I think we'll need to see more surrounding code to help further.
Another note - by allowing the existing file extension to be used, you're allowing users to upload .aspx files, which could subsequently be executed through a request. Safe filenames are GUIDs and whitelisted file extensions. Using un-sanitized uploaded path information is very dangerous. If you re-use filenames, sanitize them to alphanumerics.

how to load image with relative path in bitmap

i want to upload image int the bitmap object from asp.net, the image is location under
/uploadedimages/sampleimage.jpg
whenever i use below code to load image in bitmap, i gets error saying Parameter not valid.
Bitmap b = new Bitmap("/uploadedimages/sampleimage.jpg") // this path is coming from database holded in variable
i tried to replace the slashes in path to "\" still that does not work.
can anyone tell me what could be the reason for the error and the possible resolution.
Use Server.MapPath. And it's a good practice to use the tilde char ~ to specify the web application root.
Bitmap b = new Bitmap(Server.MapPath("~/uploadedimages/sampleimage.jpg"));
if uploadedimages directory is in your App_Data folder then you should append the App_Data absolute path to your path:
Bitmap b = new Bitmap(Path.Combine(Server.MapPath("~/App_Data"), "/uploadedimages/sampleimage.jpg"));
You can use server.MapPath, pass Url string as given below.
Server.MapPath("../images/image.gif")

How can i get all files without file extension into an array

How can i get all files without file extension into an array. I will supply the folder path.
Is this possible using Directory.GetFiles() or DirectoryInfo.GetFiles()?? Is there any alternative way?
I am using ASP.NET C#.
I would guess:
string[] files = Directory.GetFiles(dir,"*.")
(now verified; that works fine) - note that you may need to use Server.MapPath to switch between relative site paths and physical disk paths, and that the results of Directory.GetFiles are full paths.
If you need to get just the name part of all files in a folder (even those with extensions):
string[] files = Directory.GetFiles(dir,"*.*")
.Select(n => Path.GetFileNameWithoutExtension(n))
.ToArray();

Resources