I have a form which uploads an image to the server, and I am abl to get the filename of the image and other data inside the form.
How would I convert the image to string to be included in a html email?
This is the code which gives me my filename.
string PostedFileName = "not_set";
foreach (string fileName in HttpContext.Current.Request.Files)
{
HttpPostedFile file = HttpContext.Current.Request.Files[fileName];
PostedFileName = fileName;
}
MessageSent("TRUE - " + PostedFileName);
Related
This question was migrated from Super User because it can be answered on Stack Overflow.
Migrated 14 days ago.
This is a C# code for a Web Form page named "upload.aspx" which allows the user to upload a PDF file and store it in the server. The file uploaded must be a PDF file, and all the text boxes must be filled before submission. The file name is saved in the database as a string with the format of "GUID.pdf", Guid.NewGuid().ToString().
tTR.FileName = Guid.NewGuid().ToString() + ".pdf";
dataBaseDataContext.TTRs.InsertOnSubmit(tTR);
dataBaseDataContext.SubmitChanges();
String path = Server.MapPath("Attachments/" + tTR.FileName);
FileUploadtoServer.SaveAs(path);
Response.Write("<script>alert('Successfully Inserted!')</script>");
Invoice.Text = "";
Manufacture.Text = "";
HeatCode.Text = "";
Description.Text = "";
PO_Number.Text = "";
i created a search function that allows the user to search for based on Heat Code, Item Code, PO Number, and Description. he user can also edit and download the file by clicking on the "Edit / Download" link. The problem here is in my Download button. It's not downloading/ able to read the path. i can see the pdf exist in the Attachment folder but it's not able to find the correct file name associated to the heat code.
private void BindData(List<TTR> Data)
{
try
{
if (Data.Count > 0)
{
StringBuilder append = new StringBuilder();
foreach (TTR tTR in Data)
{
string PdfGuid = tTR.FileName;
append.Append("
}
tdList.InnerHtml = append.ToString();
}
}
catch (Exception)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string filePath = Server.MapPath("~/Attachments/" + filename + ".pdf");
byte[] content = File.ReadAllBytes(filePath);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename + ".pdf");
Response.Buffer = true;
Response.BinaryWrite(content);
Response.End();
}
catch (Exception)
{
This is a C# code for a web form page named "upload.aspx" that allows a user to upload a PDF file, store it on the server, and save its file name in the database as a string with the format "GUID.pdf", where GUID is a unique identifier generated using Guid.NewGuid().ToString(). Before submission, all the text boxes must be filled, and the uploaded file must be a PDF.
The code also includes a search function that allows the user to search for files based on Heat Code, Item Code, PO Number, and Description. The user can also edit and download the file by clicking on the "Edit / Download" link. However, the download button is encountering issues as it is not able to find the correct file name associated with the heat code and download the file.
The problem seems to be with the code that retrieves the file path, reads the contents of the file, sets the content type as "application/pdf", and writes the contents to the response stream. The code is trying to read the file from the server's "Attachments" folder using the file path, but it seems to be encountering issues finding the correct file.
I want to save embedded images from outlook. After my analysis i found that user can embed images, and also PDF files etc. But in case of embedding PDF or other than image files(In outlook by using Insert menu and then Object option) the mail item attachment object also contains a .mso file(An MSO file is sent with email attachments and contains information that allows the attachment to be rendered within the email message) which i am not sure like i need to save it or not.
1)So is there any need to save or process the mso file or can i just ignore it and save all other file types?
2)Based on my observation even if user embed pdf file it will be attached as image file with .png extension in outlook inbox. So can i convert all those images to .gif as shown below? Will it failure in any scenario?
if (item.Attachments.Count > 0)
{
for (int j = 1; j <= item.Attachments.Count; j++)
{
string value = item.Attachments[j].PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E");
if (!String.IsNullOrEmpty(value))
{
index++;
string tempFilename = "C:\\TestFileSave\\" + "inlineimg_" + index.ToString() + System.IO.Path.GetExtension(item.Attachments[j].FileName);
if (System.IO.Path.GetExtension(item.Attachments[j].FileName) == "mso")
{
continue;
}
item.Attachments[j].SaveAsFile(tempFilename);
string mimeType = MimeMapping.GetMimeMapping(tempFilename);
if (mimeType.Contains("image/"))
{
System.Drawing.Image Image1 = System.Drawing.Image.FromFile(tempFilename);
Image1.Save("C:\\TestFileSave\\" + "inlineimg_" + index.ToString() + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
Image1.Dispose();
System.IO.File.Delete(tempFilename);
}
}
}
}
I have link of a PDF file located on a website e.g. (https://www.mysite.com/file.pdf).
What I need is prompt the user (on Client side) the SaveAs box to choose the file location to save the file.
I tried the SaveFileDialog , but got know that it is only for Windows Forms. and my application is web based.
C# Code
var fileNumber = lblFileNumber.Text;
string fileDownloadLink = "http://www.mysite.com/" + fileNumber + ".pdf";
string fileName = fileNumber + ".pdf";
bool exist = false;
try
{
var request = (HttpWebRequest)System.Net.WebRequest.Create(fileDownloadLink);
using (var response = (HttpWebResponse)request.GetResponse())
{
exist = response.StatusCode == HttpStatusCode.OK;
}
}
catch
{
}
if (exist)
{
var wClient = new WebClient();
wClient.DownloadFile(fileDownloadLink, fileName);
}
I wrote the above code, it does check if the file is exist on the file location, but
how to prompt user to choose the location to where he want to save file and save on his local hard drive?
It is not possible to do that. When the user wants to download a file, the browser will decide how to show it to the user.
Write a file from disk directly:
HttpContext.Current.Response.TransmitFile(#"C:\yourfile.pdf");
Or from another source, loaded as a byte array:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.BinaryWrite(bytesOfYourFile);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
What I am trying to achieve is changing the name of uploaded images to a somewhat unique string.
I have created an image gallery which is populated dynamically by database. The gallery is working fine except, that files stored in the database will be problematic if an image with the same name is uploaded!
Code in the controller for file upload:
string AdvertImage = picture1.FileName;
advert.AdvertImage = AdvertImage;
var image1Path = Path.Combine(Server.MapPath("~/Content/Images"), AdvertImage);
picture1.SaveAs(image1Path);
The code below is what I am working on
string BackImage = DateTime.Now.ToString("yyyyMMddhhmmssfffffff");
caradvert.BackImage = BackImage;
var image3Path = Path.Combine(Server.MapPath("~/Content/CarImages"), BackImage);
picture3.SaveAs(image3Path);
I have managed to create a unique file name thou it is not appending .jpg to the end of the image.
Any help or advice on the subject welcome
Try this:
//save file in folder
if (FileUpload1.PostedFile.ContentType.ToLower().StartsWith
("image") && FileUpload1.HasFile)
{
string savelocation = Server.MapPath("savedImages/");
string fileExtention = System.IO.Path.GetExtension(FileUpload1.FileName);
//creating filename to avoid file name conflicts.
string fileName = Guid.NewGuid().ToString();
//saving file in savedImage folder.
string savePath = savelocation + fileName + fileExtention;
FileUpload1.SaveAs(savePath);
}
Is there any way to store HTML fragments in the Windows clipboard containing <img...> elements that reference temporary image files and to make sure that no temporary image files are left in the TEMP directory?
Here is some sample code for illustrating the problem that I am encountering:
Pressing a button in my test application executes the following code:
string tempFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()) + ".png";
CreateImage(tempFileName);
string html = string.Format("<p>[This is <b>HTML</b> with a picture:<img src=\"file://{0}\">]</p>", tempFileName);
WriteHtmlToClipboard(html);
MessageBox.Show("Wrote HTML to clipboard");
CreateImage() renders and saves an image file on the fly, like this:
private static void CreateImage(string tempFileName)
{
Bitmap b = new Bitmap(50, 50);
using (Graphics g = Graphics.FromImage(b))
{
g.DrawEllipse(Pens.Red, new RectangleF(2, 2, b.Width - 4, b.Height - 4));
Bitmap b2 = new Bitmap(b);
b2.Save(tempFileName, ImageFormat.Png);
}
}
WriteHtmlToClipboard() writes the HTML fragment to the clipboard:
private static void WriteHtmlToClipboard(string html)
{
const string prefix = "<html>head><title>HTML clipboard</title></head><body>";
const string suffix = "</body>";
const string header = "Version:0.9\r\n" +
"StartHTML:AAAAAA\r\n" +
"EndHTML:BBBBBB\r\n" +
"StartFragment:CCCCCC\r\n" +
"EndFragment:DDDDDD\r\n";
string result = header + prefix + html + suffix;
result = result.Replace("AAAAAA", header.Length.ToString("D6"))
.Replace("BBBBBB", result.Length.ToString("D6"))
.Replace("CCCCCC", (header + prefix).Length.ToString("D6"))
.Replace("DDDDDD", (header + prefix + html).Length.ToString("D6"));
Clipboard.SetText(result, TextDataFormat.Html);
}
I now have two alternatives regarding the handling of the temporary image file:
Delete the image file when the application is terminated. Problem: When I paste the HTML text from the clipboard into another application, the images are lost. Most users expect that the clipboard's content is preserved even if you close an application.
Leave the image file in the TEMP directory even after the application terminates. Problem: Who removes the image file when the clipboard's content is replaced by something else?
Of course, I could implement a helper application that runs whenever Windows boots and cleans up any temporary image file, but I would prefer a more elegant solution.