Webmatrix - WebImage helper and Create Directory - razor

I have previously successfully managed to upload a file using the webimage helper, but i am now trying to combine that with creating a directory, and failing miserably. here is my code:
if(IsPost){
//Create Directory using PropertyID
var imageroot = Server.MapPath("~/Images/Property/");
var foldername = rPropertyId.ToString();
var path = Path.Combine(imageroot, foldername);
if(!Directory.Exists(path)){
Directory.CreateDirectory(path);
}
photo = WebImage.GetImageFromRequest();
if(photo != null){
MediumFileName = rPropertyId + "_" + gooid + "_" + "Medium";
imagePath = path + MediumFileName;
photo.Save(#"~\" + imagePath);}
}
First, i create a directory with the name of the propertyID. This works fine. I then try and upload new photo's into that path, and i get an error saying that "The given path's format is not supported".
Any ideas?

You correctly use Path.Combine() when creating the directory path, you should do the same when making the image path.
imagePath = Path.Combine(path, MediumFileName);
Other than that, the error message suggests that perhaps it is the omission of a file extension that is causing issues? Perhaps use Path.GetFileName(photo.FileName) or similar and use that as the end of your constructed pathname.

Related

Uploading files on Webapp into Docker container

I have been googling constantly and can't seem to find an answer for this. My apologies if it's a noob question as I am still extremely new to Docker.
Scenario:
I have a dockerized web app with a NodeJS backend. On the website, I need to be able to upload files and store the path to them in the database (MSSql Database container). When I upload files using multiparty on the Node end, it gives it the temp path for files. On Windows, its ...../AppData/Local/Temp/.... On Linux, it looks to be /tmp/... Then, I use this path as a link to open the file from a different page. On Windows, running the app locally (no dockerizing), I can access the file (excluding Chrome's security features that prevent the download). However, on Linux and dockerized, there is no file. I will attach my file uploading code at the bottom.
I know that docker containers do not talk to the host's file system like a normal web application. My file isn't being stored in the /tmp folder as I've already checked there. My guess is that it is somehow storing it within the container.
My confusion lies with how to store these files. Volumes seem to be for loading files into containers and storage drivers don't seem to be something you can mess with other than to configure them (I am using overlay2 if it matters). How do I store the uploaded files within my container so that I can store their path and access them again later?
var app = express();
app.post("/api/files", function(req, res) {
var form = new multiparty.Form();
form.parse(req, function(err, fields, files) {
var dict = files.Contents[0];
console.log(dict);
var query = "EXECUTE CTF.dbo.CTF_CreateFilesSp " + fields.ID + ", '" + dict["originalFilename"] + "', '" + dict["path"] + "'";
executeQuery(query, res);
});
});
UPDATE:
I was able to get a volume/bind mount set up for my container using:
volumes:
- /var/opt/tmp:/var/opt/tmp
and I updated my parsing method to this:
app.post("/api/files", function(req, res) {
var form = new multiparty.Form();
var target_path = '';
form.on('file', function(name, file) {
var tmp_path = file.path;
target_path = '/var/opt/tmp/' + file.originalFilename;
fs.copyFile(tmp_path, target_path, function(err) {
if (err) console.error(err.stack);
else console.log(target_path);
})
});
form.parse(req, function(err, fields, files) {
var dict = files.Contents[0];
var query = "EXECUTE CTF.dbo.CTF_CreateFilesSp " + fields.ID + ", '" + dict["originalFilename"] + "', '" + target_path + "'";
executeQuery(query, res);
});
});
When I upload the file, I do see a copy of this show up in the host's directory as expected. However, when I click the link to the file on an html page, using the full path /var/opt/tmp/FILENAME, it says cannot GET /var/opt/tmp/FILENAME.
I'm sure this has to do with incorrect permissions or href in my html, but I'm not sure. I'm very new to web development.
How do I get the link on an html page to download this file from the directory? Please let me know if this is out of the scope of the original question and I'll make a new one.

How to upload a local file as link in asp.net

I want to know how to add a local file path as a link and after adding it i want to download the file while clicking the link in asp.net.
My code:
<a href="D:/Sample/test.html" runat="server">
Here i just add my local path to the server.But here nothing done while clicking the link. I want to use .zip file instead of .html file.Let me know how to upload and download by using a link.Thanks in advance
I fail to see the problem here. Just add "~/" to find file from the root of your project and add runat="server" to the anchor link:
Download Zip File
You need to resolve it from the root because while you may know that it's on the D drive on your local machine, you cannot be sure that will be the same on the server. And even if it is on the same drive on the server, what if someone migrates it later on?
As for uploading a file, simply use the Upload control?
There are lots of situation, Using this code you can do it.
File Upload Code
string FilePath = "";
string[] a = new string[1];
string fileName = "";
string FullName = "";
if (FileUploader.FileName.Length > 0)
{
a = FileUploader.FileName.Split('.');
fileName = Convert.ToString(System.DateTime.Now.Ticks) + "." + a.GetValue(1).ToString();
FilePath = Server.MapPath(#"~\SavedFolder");
Fup1.SaveAs(FilePath + #"\" + fileName);
FullName = FilePath + #"\" + fileName;
// Database Saved Code
}
File Download Code
string filename = "filename from Database";
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
string aaa = Server.MapPath("~/SavedFolder/" + filename);
Response.TransmitFile(Server.MapPath("~/SavedFolder/" + filename));
Response.End();

Getting error when converting multipage fillable PDF to html form using Abcpdf

The exception thrown was " at WebSupergoo.ABCpdf10.Doc.Save(String path)
at GetHtmlFromUploadedPdfDocument(Nullable`1 pageNumber) in....." .
The uploaded pdffile contains barcodes and fillable fext fields .
Below is the code i used to convert pdf to html.
var filePaths= HttpContext.Current.Server.MapPath("~/PDF//");
byte[] bytes = File.ReadAllBytes(filePaths);
doc.Read(bytes);
if (pageNumber > 0)
{
doc.PageNumber = pageNumber.Value;
doc.RemapPages(pageNumber.ToString());
}
var pdfFile = "sample";
var htmlPath = HttpContext.Current.Server.MapPath("~/HTML/" + pdfFile + ".html");
doc.Encryption.CanChange = false;
doc.Encryption.CanEdit = false;
doc.Encryption.CanAssemble = false;
doc.Encryption.CanExtract = false;
doc.Encryption.CanFillForms = false;
doc.Save(htmlPath);
content = File.ReadAllText(htmlPath);
I know this is old post. But i faced similar issue.
I solved it eventually, it might help others.
In my case, folder under which I was saving the file was missing proper permission.
Please perform following task:
Right click on root folder where you are trying to save file.
Select Properties. Uncheck Read-only in attributes section.
Go to Security Tab. Select Edit > Add.
Key in "Everyone" in text box. Then Check Names > Ok.
Give "Everyone" Read, Write and Modify permission.

Google-drive-java-api-returns-deleted-files

I have tried to get push notification for when i am editing that drive files.
Everything was fine until I tried to delete these folders from Google Drive UI. They disappeared from the UI, but my service still receives them as if they were present.
try {
configdata = dao.getConfigByChannelId(channelId,IntegrationType.DRIVE);
System.out.println("ACCESS TOKEN FOR CHANNEL ID: " + configdata.getAccessToken());
GoogleCredential credential = new GoogleCredential().setAccessToken(configdata.getAccessToken());
Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
.setApplicationName("Akoonu")
.setHttpRequestInitializer(credential).build();
Files.List files = service.files().list();
try {
Change change = service.changes().get(String.valueOf((Integer.parseInt(changeId) - 1))).execute();
System.out.println("Changed file ID: " + change.getFileId());
System.out.println("Check delete case: " + change.getDeleted());
if (change.getDeleted()) {
System.out.println("File has been deleted");
File changedFile = change.getFile();
strpath.replace(changedFile.getTitle(), "");
String path = strpath.replace(changedFile.getTitle(), "");
//deleteItem = iao.getIventoryItemByFilePathAndConfigId(changedFile.getTitle(), path, configdata.getId(), configdata.getAccountId());
deleteItem = iao.getIventoryItemByExternalId(changedFile.getId(), configdata.getId(), configdata.getAccountId());
itemService.deleteInventoryItem(deleteItem.getId(), deleteItem.getAccountId());
//deleteFilePathList.add(metadata.getPathDisplay().substring(1));
} else {
File changedFile = change.getFile();
System.out.println("Changed file Title: " + changedFile.getTitle());
.
.
.
.
.
I have tried lot of samples but still not fixed.Plz help me.Thanks
I am also facing this type of issues on my project but after a long time i got a solution .
Please use query String parameter in your code like
Files.List request = service.files().list().setQ("trashed=false");
..
Surely it will fix that issue.
THanks.
In v3 there's a "explicitlyTrashed" trashed flag, and that was what solved it for me (for now)
https://developers.google.com/drive/api/v3/reference/files

Error #2035 URL not found - file path configuration

I try to open a swf from another swf. They are on the same folder. Here is the code block I am trying to get the file.
var newRequest:URLRequest;
var path:String = "mahmut"+".swf";
newRequest = new URLRequest(path);
newLoader = new Loader;
newLoader.load(newRequest);
newLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeFn);
newLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, camaluriFn);
function camaluriFn(e:IOErrorEvent)
{
trace("Error: " + String(e));
}
and trace throws Error #2035: URL Not Found. URL: app:/mahmut.swf
So how can I give right path and it can open display successfully?
Doesn't matter where those files are, what matters is from where they are loaded. In your case all local paths are from the app root no matter what. If you swf are in folder "myswfs" then the right path is not just "mahmut"+".swf" but "myswf/smahmut.swf".

Categories