ASP File upload control - asp.net

I am using dynamic ajax file upload control.
AjaxControlToolkit.AsyncFileUpload()
While i am trying to upload file, in page load the
value of Request.ServerVariables["QUERY_STRING"] is, "AsyncFileUploadID=MainContent_flbFileUpload1&rnd=01846502097323537".
Can anyone tell me, why this happens?

In AsyncFileUpload.pre.js file found in ajaxcontroltoolkit.zip and in _onload: function (e) { .... } has below sample lines which create the resulting postback URL as:
mainForm.action = url + 'AsyncFileUploadID=' + this.get_element().id + '&rnd='
+ Math.random().toString().replace(/\./g, "");
mainForm.target = this._iframeName;

Related

ASP.NET Cant Open link via email

I created an email in ASP.NET and I want to add a link to the body, but this not a normal url, its a file that is created via byte array and now I want that file to be linkable in my email, but no matter what I do the link is clickable but nothing opens, here is my code:
FileContentResult eventPass = new FileContentResult(generatedPass, "application/vnd.apple.pkpass");
eventPass.FileDownloadName = "preview.pkpass";
message += "<a href='//" + eventPass + "' target='_blank'>Click Here</a>";
AlternateView alternateView = AlternateView.CreateAlternateViewFromString(message, null, MediaTypeNames.Text.Html);
alternateView.LinkedResources.Add(inline);
email.AlternateViews.Add(alternateView);
email.IsBodyHtml = true;
email.Headers.Add("Content-Type", "application/vnd.apple.pkpass");
I know the file is generated correctly because if I return eventPass the file downloads.
Do I need to save eventPass to the server?
Possible answer here:
How to set mime type of application/vnd.apple.pkpass in order to share pass by link or email
Please mark my answer as correct if this has helped you.

Hard coded path: how do I link it on a ASP.NET MVC website?

I have a PDF document in the following location on the remote web server:
C:\Domains\Bin\Invoices\1000.pdf
How do I link it? I tried:
string rootPath = Server.MapPath("~");
string path = Path.GetFullPath(Path.Combine(rootPath, "..\\Bin\\Invoices"));
<p>#Html.Raw("<a href='"+ path + "\\" + "1000.pdf'>Original PDF copy</a>")</p>
Without success: the browser tooltip shows me file:///C:/domains/bin/invoices/1000.pdf
Thanks.
EDIT
Solved using Joe suggestion, this way:
public FileResult InvoiceInPdf(string id)
{
string rootPath = Server.MapPath("~");
string path = Path.GetFullPath(Path.Combine(rootPath, "..\\Bin\\Invoices\\"));
return File(path + id + ".pdf", "application/pdf");
}
You can't create a hyperlink to browse to a file at an arbitrary location on the server. Which is a good thing, as the security of your web server would otherwise be compromised.
You should create an Action that streams the file you want by returning a FileResult. Google will provide you with examples of this approach.

ASP.NET Resize image before saving?

Below is my code to upload and image and save it to be later displayed in a GridView,
How can I resize the image to a small one before saving it if it is too large?
if (this.fuUpload.HasFile == true)
{
string path = Server.MapPath(#"~\images");
string filename = this.fuUpload.FileName;
this.fuUpload.SaveAs(path + #"\" + filename);
this.image.ImageUrl = #"\images\" + filename;
}
Take a look at ImageResizer for ASP.NET:
http://imageresizing.net/
You can use WebImage class built in System.Web.Helpers assembly. Take a look at Working with Images in an ASP.NET Web Pages guide.

ASP.NET Photo Upload

When uploading a photo to my server using the following code, I'm receiving an erroneous value. This is working fine in the debug mode and when published in localhost.
string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), date);
if (!Directory.Exists(HttpContext.Server.MapPath("../Uploads")))
{
Directory.CreateDirectory(HttpContext.Server.MapPath("../Uploads"));
}
file.SaveAs(filePath);
Can someone please point out what I've done incorrectly?
ok I am assuming that you are using File Upload control or you can use the below sample code if you want to use the FileUpload control in your asp.net page.
Add the FileUpload control (Here im adding ajax async FileUpload control and named as asyncFileUpload.
Write A Method and Call it whenever you want.
public int AsyncFileUpload()
{
string xlsFile = AsyncFileUpload1.FileName;
if (AsyncFileUpload1.HasFile)
{
string FileName = Path.GetFileName(AsyncFileUpload1.PostedFile.FileName);
string Extension = Path.GetExtension(AsyncFileUpload1.PostedFile.FileName);
string FilePath = Server.MapPath("~/Uploads/" + FileName);
if (Extension == ".doc")//check the file extension here
{
AsyncFileUpload1.SaveAs(FilePath);
}
}
}

Add filter to FileUpload Control

How to add filter to the fileupload control in asp.net? I want a filter for Word Template File (.dot).
You could also do a javascript alternative to filtering it server side (you'd probably want to do that as well) but this saves the client from spending the time waiting on an upload to finish just to find out it was the wrong type.
http://javascript.internet.com/forms/upload-filter.html
So basically you just run a javascript function on submit that parses off the extension of the uploaded file and gives them an alert if its not of the right type.
You could also use document.forms[0].submit(); instead of passing the form reference through (as ASP.NET really only uses a single form (unless your doing something funky))
string fileName = fuFiles.FileName;
if(fileName.Contains(".dot"))
{
fuFiles.SaveAs(Server.MapPath("~/Files/" + fileName));
}
If you mean to filter the file extensions client/side, with the standard browser's file selector, isn't possible.
To do that you have to use a mixed type of upload, such as SWFUpload, based on a flash uploader system (that's a really nice techinque: it allows you to post more than a file at time).
The only thing you can do in standard mode is to filter the already posted file, and I suggest to use System.IO.Path namespace utility:
if (Path.GetExtension(upFile.FileName).ToUpper().CompareTo(".DOT") == 0)
{
/* do what you want with file here */
}
Check the filename of the uploaded file serverside:
FileUpload1.PostedFile.FileName
Unless you want to use java or something similar on the client, there's really not much you can do for filtering uploaded files before they're sent to the server.
Here I have a small method that I used to filter which types of files can be uploaded by the fileupload control named fuLogo.
if (fuLogo.HasFile)
{
int counter = 0;
string[] fileBreak = fuLogo.FileName.Split(new char[] { '.' });
logo = Server.MapPath("../Images/Logos/" + fileBreak[0] + counter.ToString()+ "." + fileBreak[1]);
if (fileBreak[1].ToUpper() == "GIF" || fileBreak[1].ToUpper() == "PNG")
{
while (System.IO.File.Exists(logo))
{
counter++;
logo = Server.MapPath("../Images/Logos/" + fileBreak[0] + counter.ToString() + "." + fileBreak[1]);
}
}
else
{
cvValidation.ErrorMessage = "This site does not support any other image format than .Png or .Gif . Please save your image in one of these file formats then try again.";
cvValidation.IsValid = false;
}
fuLogo.SaveAs(logo);
}
basically, I first Iterates through the directory to see if a file already exists. Should the file exist, (example picture0.gif) , it will increase the counter (to picture1.gif). It prevents that different users will overwrite each other's pictures should their pictures have the same name.

Resources