Website Not Downloading PDF Created By Site C# / ASP.NET MVC - c#

I have a website deployed on Hostgator. This is my first site so I'm still a little new to all this. Using Visual Studio 2019 this works completely fine in development but once deployed on my site it throws an error:
.../CreateRateConfirmation?loadNumber=RT222398 500 (Internal Server Error) VM1795 jquery:1
I'm using itext v.7 to create the pdf, and I'm thinking it might have to do with my path when creating the pdf? The actual pdf function is over 1800 lines of code so I am not going to post all that, but this is the relevant (I think) info:
string fileName = loads[i].Carrier.carrierName + " " + loadNumber + " Rate Confirmation.pdf";
string desktopFolder = "C:";
System.IO.Directory.CreateDirectory(desktopFolder + "\\Rate Confirmations");
string downloadFolder = desktopFolder + "\\Rate Confirmations";
PdfWriter writer = new PdfWriter(downloadFolder + "\\" + fileName);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
... roughly a billion lines of pdf creation...
document.Close();
System.Diagnostics.Process.Start(downloadFolder + "\\" + fileName);
string timeStamp = getTimeStamp(name);
loads[i].notes = loads[i].notes + timeStamp + ": Rate Confirmation Created^";
db.Entry(loads[i]).CurrentValues.SetValues(loads[i]);
db.SaveChanges();
break;
For what it's worth the function has a timestamp in it that should show up in the notes on my site as well and it's not, so it clearly is not making it all the way through this function.
I'm sure this is a completely easy solution but I'm really new to all this and I just can't seem to find a solution.
Thanks in advance!!!

I am not exactly sure about what is causing the problem. But there is certainly a thing which may be causing PDF generation to fail. Never hardcode directory manually. Instead try to get it dynamically according to the server on which your web application is deployed
You can replace the directory code with
string desktopFolder = Path.GetFullPath(Directory.GetCurrentDirectory());
string downloadFolder = desktopFolder + "\\Rate Confirmations";
if (!Directory.Exists(downloadFolder))
Directory.CreateDirectory(downloadFolder);
This code will automatically get you application current directory and change it to absolute path from virtual path

Related

Some times PDF files get corrupted after File Upload C#

I am trying to upload PDF files. Only sometimes uploaded file gets corrupt. When I open that file on Adobe or browser. its says Insufficient data for an images
This problem is not for all the uploads. When I tried to upload the same file again its works perfectly.
I am not able to replicate the issue so that I can know why this type of issue occurring
I am using below code to save the file
FileUpload upload = GridView1.Rows[index].FindControl("FileUpload1") as FileUpload;
if (upload.HasFile)
{
string nameoffile = upload.FileName;
Random ran = new Random();
int forReference = ran.Next();
string[] strfileArray = nameoffile.Split('.');
nameoffile = strfileArray[0] + "" + forReference + ".pdf";
upload.SaveAs(path + "/" + nameoffile);
}
else
{
upload.SaveAs(path + "/" + nameoffile);
}
Note: We are running this appliaction on Azure VM. This code was works without any issue on previous server. since we migrated to Azure
We had hosted application on C drive of azure VM. After Changing application to secondary storage i.e. D drive problem got resolved

Problems Downloading from FTP

I'm trying to download a FTP file from one of our clients, but in production it fails with this message: "Operation timeout exceeded", but if you try to open the FTP location from browser, it opens without problem. During develop this happened because of the proxy, but also blocked the browser. In production, the proxy was shutdown but still fails. I need help with this.
This is the code for the download
using (WebClient vloftpClient = new WebClient())
{
vloftpClient.Credentials = new System.Net.NetworkCredential(vlcUsuario, vlcClave);
vloftpClient.DownloadFile(#vlcUbicacionOrigen + #"/" + vlcNombreArchivoOrigen, pvcUbicacionDestino + #"/" + pvcProveedor + "_" + vlcNombreArchivoDestino);
}
WebRequestClient offers a better solution for working such as mode of transfer.
so use as below
FtpWebRequest objFtp;
objFtp.Method = ...DownloadFile
objFtp.ReadWriteTimeout = objFtp.Timeout = 'a value in terms of millisecond'
at last , try to download the files

Set up Scheuled Task to create Excel File and send email with attachment in C# Console application

I had a problem where a scheduled task would not send out an email with an attachment in a C# console application. It took me a long time to figure out the solution (it is working now), so I wanted to share what the two issues were so others can find the solution easily.
There were two problems:
1) When saving the file to the directory I could not use a relative directory like:
DirectoryInfo currentDir = new DirectoryInfo(Directory.GetCurrentDirectory());
attachmentPath = currentDir.Parent.Parent.FullName + #"\Reports\" + startDate.ToString("yyyy.MM.dd") + " - " + endDate.ToString("yyyy.MM.dd") + ".xlsx";
I had to explicitly set the directory like:
attachmentPath = #"E:\reports\Excel Automated Reports\WebEx Daily Report\ExcelReportAutomation\Reports\" + startDate.ToString("yyyy.MM.dd") + " - " + endDate.ToString("yyyy.MM.dd") + ".xlsx";
After, I did that the file was being created and saved but still was not emailing it out. If I sent out an email without an attachment then the email would go out. The way to fix this issue is by setting the Start in (optional) option in the edit action for the scheduled task.
I repeat this is not a question but I am trying to lay out a solution for people who come across the same issue.

Zip File Entry has custom file extension. C# claims it cannot open

Solution Found.
Thanks to everyone helping me, I found out what the root problem was. The .trl file had nothing to do with it. It was the path being created wrong. I was doing "TRLR" + Path, when it should have been "TRLR" + fileName. This was a stupid error on my part, and I apologize for wasting your time, but I appreciate the help!
I have a zip file given to us by a 3rd party. In this zip files are custom files. These are just text files with a different extension, which I assume is just to frustrate me.
I'm trying to open this files in my C# application, but it keeps throwing the error that the format is not supported.
Since these are just text files, I feel there must be some way for this to happen.
If anyone has any ideas, please let me know.
Code:
using (ZipArchive archive = ZipFile.OpenRead(_trailerName))
{
ZipArchiveEntry entry = archive.GetEntry(tableChanged + ".trl");
Stream ms = entry.Open(); //Here is what's causing the issue.
StreamReader reader = new StreamReader(ms);
string allLinesRead = reader.ReadToEnd();
string[] everyCell = allLinesRead.Split('|');
int numRecords = Convert.ToInt32(everyCell[1]);
int numChanged = getRecordNum(tableChanged);
Console.Write(numRecords + "/" + numChanged + " - " + tableChanged);
if (numChanged != numRecords)
{
_errorMessage += "Records updated do not match trailer. (" + numRecords + "/" + numChanged + ") Please check database. Table affected: " + tableChanged + Environment.NewLine;
}
}
Error:
The given path's format is not supported.
I know this is specific, but I need advice on what steps I can take to resolve this.
Thanks.
The native zip functionality of .NET is frequently lacking in terms of the ability to handle and modify zip files created by applications other than the windows zip tool. While the "zip" file is standardized, you still see a decent amount of variation on file headers and attributes.
I would suggest you look into DotNetZip (Ionic), which is a third party library that has very robust capabilities in terms of creating and opening zip files. I've found it to be much more forgiving and capable than the basic functionality that .NET gives you, and the code to open a zip is extremely similar to what you have.

Dynamically opening a doc file c#

I have been using this line of code for the past few weeks to dynamically open a doc file once the user has created it and its been working fine...
System.Diagnostics.Process.Start(#"C:\\Users\\peter\\Desktop\\" + txtEditTitle.Text + ".doc");
but today, for some reason it gives me the following error:
Can anybody help?
As you can read in the exception the file is not there. It would not be wrong to check if the file exists before opening it.
As per your screenshot, it shows file is not there. Check your path for the file and make sure file exists:
string strPath = "C:\\Users\\peter\\Desktop\\" + txtEditTitle.Text + ".doc";
// strPath=#"C:\Users\peter\Desktop\" + txtEditTitle.Text + ".doc";
if (File.Exists(strPath))
{
System.Diagnostics.Process.Start(strPath);
}

Categories

Resources