Caching Files in the Browser - asp.net

By using the below function I am caching js, css file in the browser.
Like wise I want to cahe the Image in the browser.
private static void CacheOrFetchFromServer(string relativePath, string absolutePath, HttpContext context)
{
Cache cache = HttpRuntime.Cache;
string content;
if (cache[relativePath] == null)
{
Encoding encoding = Encoding.GetEncoding(DefaultEncodingCodePage);
CacheDependency dependency = new CacheDependency(absolutePath);
content = File.ReadAllText(absolutePath, encoding);
cache.Insert(relativePath, content, dependency);
}
else
{
content = cache[relativePath].ToString();
}
using (StreamWriter sw = new StreamWriter(context.Response.OutputStream))
{
sw.Write(content);
}
}
I had tried the below one to cache the image. But it didn't show the image.
private static void CacheOrFetchImageFileFromServer(string relativePath, string absolutePath, HttpContext context)
{
string extension = System.IO.Path.GetExtension(relativePath);
if (extension.ToUpper() == ".JPG" || extension.ToUpper() == ".PNG" || extension.ToUpper() == ".GIF" || extension.ToUpper() == ".TIFF")
{
Cache cache = HttpRuntime.Cache;
System.Drawing.Image imgPhoto = null;
if (cache[relativePath] == null)
{
Encoding encoding = Encoding.GetEncoding(DefaultEncodingCodePage);
CacheDependency dependency = new CacheDependency(absolutePath);
FileStream fs = File.OpenRead(absolutePath);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
MemoryStream ms = new MemoryStream(data);
Bitmap bmp = new Bitmap(ms);
imgPhoto = System.Drawing.Image.FromFile(absolutePath);
cache.Insert(relativePath, bmp, dependency);
}
else
{
imgPhoto = (Image) cache[relativePath];
}
context.Response.Write(absolutePath);
//using (StreamWriter sw = new StreamWriter(context.Response.OutputStream))
//{
// sw.Write(absolutePath);
//}
}
}

I'm not sure I understand what you're doing here.
First of all, the Cache object in asp.net is used to cache data on the server side, not on the client side (browser).
Caching of files, specially css, JavaScript and images, is done by the browser automatically, you don't have to do this manually for every file. And even if you had to do this manually, this isn't the way - it looks like you're just creating a copy of the file on the server's cache (I havn't done tests, but I trust Microsoft and assume this is already done in some way, and your way is actually slower).
If you want greater control over client side caching, you can enable content expiration on the IIS.

Related

Crystal report method not found

I made a feedback project. I made it on ASP.NET MVC 5 it also has crystal reports. reports were working fine, but suddenly they stopped to work. I don't what happened with them. but since last week I tried hard to find solution but unfortunately could not get the right one who solved the solution. I downloaded different run times but all went vain. this is the bottom line of error.
"Method not found: 'CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag CrystalDecisions.ReportAppServer.ReportDefModel.ISCRExportOptions.get_ExportOptionsEx()'"
this is the code:
public CrystalReportFeedback UserFeedbackDateWise(FeedbackReport be){
if (Session["CurrentUser"] != null && Convert.ToInt32(Session["User_Id"]) != 0)
{
string reportPath = Path.Combine(Server.MapPath("~/Reports"), "UserFeedbackReport.rpt");
if (ModelState.IsValid)
{
be.FromDate = Convert.ToDateTime(TempData["UserFromDate"]);
be.ToDate = Convert.ToDateTime(TempData["UserToDate"]);
be.User_Id = Convert.ToInt32(Session["User_Id"]);
}
return new CrystalReportFeedback(reportPath, be);
}
else
{
return null;
//new CrystalReportFeedback(reportPath, be);
}
}
Init of the report :
public CrystalReportFeedback(string reportPath, FeedbackReport be)//, object dataSet)
{
//int[] array;
string strConnect = Convert.ToString(System.Configuration.ConfigurationManager.ConnectionStrings["TSC"]);
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(strConnect);
string _username = builder.UserID;
string _pass = builder.Password;
string _server = builder.DataSource;
string _database = builder.InitialCatalog;
ReportDocument reportDocument = new ReportDocument();
//
reportDocument.Load(reportPath);
reportDocument.SetDatabaseLogon(_username, _pass, _server, _database);
if (be.Region_Id != 0)
{
reportDocument.SetParameterValue("#Region_Id", be.Region_Id);
}
if (be.User_Id != 0)
{
reportDocument.SetParameterValue("#User_Id", be.User_Id);
}
reportDocument.SetParameterValue("#FromDate", be.FromDate);
reportDocument.SetParameterValue("#ToDate", be.ToDate);
//reportDocument.ExportToDisk(ExportFormatType.PortableDocFormat, "C:\report.pdf");
_contentBytes = StreamToBytes(reportDocument.ExportToStream(ExportFormatType.PortableDocFormat));
}
Export method :
public override void ExecuteResult(ControllerContext context)
{
var response = context.HttpContext.ApplicationInstance.Response;
response.Clear();
response.Buffer = false;
response.ClearContent();
response.ClearHeaders();
response.Cache.SetCacheability(HttpCacheability.Public);
response.ContentType = "application/pdf";
using (var stream = new MemoryStream(_contentBytes))
{
stream.WriteTo(response.OutputStream);
stream.Flush();
}
}
private static byte[] StreamToBytes(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
Hope that I will get my solution at earliest.
this is modified code:
[HttpGet]
public FileResult UserFeedbackDateWise(FeedbackReport be)
{
if (Session["CurrentUser"] != null && Convert.ToInt32(Session["User_Id"]) != 0)
{
string reportPath = Path.Combine(Server.MapPath("~/Reports"), "UserFeedbackReport.rpt");
if (ModelState.IsValid)
{
be.FromDate = Convert.ToDateTime(TempData["UserFromDate"]);
be.ToDate = Convert.ToDateTime(TempData["UserToDate"]);
be.User_Id = Convert.ToInt32(Session["User_Id"]);
}
string strConnect = Convert.ToString(System.Configuration.ConfigurationManager.ConnectionStrings["TSC"]);
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(strConnect);
string _username = builder.UserID;
string _pass = builder.Password;
string _server = builder.DataSource;
string _database = builder.InitialCatalog;
ReportDocument reportDocument = new ReportDocument();
//
reportDocument.Load(reportPath);
reportDocument.SetDatabaseLogon(_username, _pass, _server, _database);
if (be.Region_Id != 0)
{
reportDocument.SetParameterValue("#Region_Id", be.Region_Id);
}
if (be.User_Id != 0)
{
reportDocument.SetParameterValue("#User_Id", be.User_Id);
}
reportDocument.SetParameterValue("#FromDate", be.FromDate);
reportDocument.SetParameterValue("#ToDate", be.ToDate);
Stream stream = reportDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
//Here i have my stream with my pdf report, i just create a new FileStreamResult and return it to my client like that :
FileStreamResult myfile = new FileStreamResult(stream, "application/pdf");
return myfile;
//new CrystalReportFeedback(reportPath, be);
}
else
{
return null;
//new CrystalReportFeedback(reportPath, be);
}
}
This isn't a coding issue, it's a runtime issue. The version of the crystal runtime or the bitness of your application.
One thing to try first is to upgrade both your development version and ensure you're running the same version in production. See https://apps.support.sap.com/sap/support/knowledge/public/en/2148492 for more details
It says:
Compile your application either to 'X86 mode' or 'X64 mode'
Install the particular versions of runtimes on deployment machine.
i.e. If the application is compiled as 32 bit, then install the 32bit runtimes.
I'll try my best to help you exporting your report, but your post is not very clear. For your next post try to be very specific and provide as much information as you can.
I currently made a MVC project and export a crystalreport report from my controller to my client.
I think that your ExecuteResult method can work, but working with the httpcontext is useless, Crystalreport and .NET provide some useful methods to do the same.
So i'll show you how i create and export my report so you can copy / paste and modify your code.
Here is my controller method, called from a button :
[HttpGet]
public FileResult InitReport()
{
//I create my report here
FileImportReport rptH = new FileImportReport();
// Some configuration on the report, datasource, databaselogon .. etc
...
//
//Then I export my report to a pdf stream like that :
Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
//Here i have my stream with my pdf report, i just create a new FileStreamResult and return it to my client like that :
FileStreamResult myfile = new FileStreamResult(stream, "application/pdf");
return myfile;
}
My method is called from a button but it can work like you want, or the file can be saved in any known path.
You can test to reproduce my code, in your CrystalReportFeedback method use my code with your reportDocument object, you don't need to use your StreamToBytes method.
Regards,
EDIT : Useful links with your error :
Crystal Reports exception in Visual Studio 2013
https://www.arcanadev.com/support/kb/K00000499.aspx

Upload Image from URI to Azure BLOB

I'd like to upload images from an uri postet to an asp.net mvc5 controller to azure blob storage. I already got it working with HttpPostedFileBase, like this. Can I somehow get the memory stream from an image uri?
HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
var imgFile = System.Drawing.Image.FromStream(hpf.InputStream, true, true);
CloudBlockBlob blob = coversContainer.GetBlockBlobReference("img.jpg");
MemoryStream stream = new MemoryStream();
imgFile.Save(stream, ImageFormat.Jpeg);
stream.Position = 0;
blob.UploadFromStream(stream);
So this is how I managed to get it done:
public static Image DownloadRemoteImage(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (Exception)
{
return null;
}
// Check that the remote file was found. The ContentType
// check is performed since a request for a non-existent
// image file might be redirected to a 404-page, which would
// yield the StatusCode "OK", even though the image was not
// found.
if ((response.StatusCode == HttpStatusCode.OK ||
response.StatusCode == HttpStatusCode.Moved ||
response.StatusCode == HttpStatusCode.Redirect) &&
response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
{
// if the remote file was found, download it
Stream inputStream = response.GetResponseStream();
Image img = Image.FromStream(inputStream);
return img;
}
else
{
return null;
}
}
This code snipped was taken and modified from this question's answer:
Download image from the site in .NET/C#

Content service returns old content for some time

I'm using following snippet for saving content:
private void writeToFile(NodeRef nodeRef, String content) throws IOException {
ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
InputStream contentStream = new ByteArrayInputStream(content.getBytes(encoding));
writer.setMimetype(mimeType);
writer.setEncoding(encoding);
writer.putContent(contentStream);
Map<QName, Serializable> repoProps = nodeService.getProperties(nodeRef);
ContentData contentData = (ContentData) repoProps.get(ContentModel.PROP_CONTENT);
if(contentData == null)
contentData = writer.getContentData();
contentData = ContentData.setEncoding(contentData, encoding);
contentData = ContentData.setMimetype(contentData, mimeType);
repoProps.put(ContentModel.PROP_CONTENT, contentData);
contentStream.close();
nodeService.setProperties(nodeRef, repoProps);
}
When I read content written this way within short period of time (depends on server load) in other place, old content is returned. So it looks like that maybe indexing is in progress, so before final commit old content is returned, is that possible? If so, is it possible to override this behavior and access newest possible content? Via contentUrl?
To avoid this behavior I'm using thread for each read request, which sleeps for some time at the beginning, but I really dislike this "solution".
Edit: I built from newest SVN source, running on Tomcat 6.0.35 on Linux (CentOS and Ubuntu); system load - i mean hundreds of files changing every few seconds.
Edit: reading looks like this:
private byte[] readFileContent(NodeRef nodeRef) throws IOException {
ContentReader reader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
if(reader == null)
return null;
InputStream originalInputStream = reader.getContentInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final int BUF_SIZE = 1 << 8; // 1KiB buffer
byte[] buffer = new byte[BUF_SIZE];
int bytesRead = -1;
while ((bytesRead = originalInputStream.read(buffer)) > -1) {
outputStream.write(buffer, 0, bytesRead);
}
originalInputStream.close();
return outputStream.toByteArray();
}
Ok, solved with simplier saving like this:
ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
InputStream contentStream = new ByteArrayInputStream(content.getBytes(encoding));
writer.setMimetype(mimeType);
writer.setEncoding(encoding);
writer.putContent(contentStream);
contentStream.close();
Previous saving was at place because of some content encoding problems, so testing shows if this works.

File not found exception once deployed to Server

I am using the below code to Upload an Image file to a SharePoint Document Library. The code works fine locally but once deployed to server, i get the Exception as file not found.
String fileToUpload = FlUpldImage.PostedFile.FileName; //#"C:\Users\admin.RSS\Desktop\Photos\me_skype.jpg";
String documentLibraryName = "SiteAssets";
if (!System.IO.File.Exists(fileToUpload))
throw new FileNotFoundException("File not found.", fileToUpload);
SPFolder myLibrary = web.Folders[documentLibraryName];
// Prepare to upload
Boolean replaceExistingFiles = true;
String fileName = CheckStringNull(txtFirstName.Text) + CheckStringNull(txtLastName.Text) + CheckDateNull(txtDOB) + System.IO.Path.GetFileName(fileToUpload); ;
if (fileName.Contains('/'))
{
fileName = fileName.Replace("/", "");
}
if (fileName.Contains(':'))
{
fileName = fileName.Replace(":", "");
}
FileStream fileStream = File.OpenRead(fileToUpload);
//Upload document
SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);
string url = site.ToString() + "/" + spfile.ToString();
if (url.Contains("="))
{
url = url.Split('=')[1];
}
//Commit
myLibrary.Update();
The string fileupload contains URL as C:\Users\admin.RSS\Desktop\Photos\me.jpg This URL is actually the client system and the server side code throws exception as file not found. How to handle this issue?
UPDATE:
I removed the lines of code that checks if the file exists and now i get the exeption on FileStream fileStream = File.OpenRead(fileToUpload); as c:\windows\system32\inetsrv\20120605_133145.jpg cold not be found
Kindly help. Thank You
if (this.fuAvatarUpload.HasFile && this.fuAvatarUpload.PostedFile.FileName.Length > 0)
{
string extension = Path.GetExtension(file.FileName).ToLower();
string mimetype;
switch (extension)
{
case ".png":
case ".jpg":
case ".gif":
mimetype = file.ContentType;
break;
default:
_model.ShowMessage("We only accept .png, .jpg, and .gif!");
return;
}
if (file.ContentLength / 1000 < 1000)
{
Image image = Image.FromStream(file.InputStream);
Bitmap resized = new Bitmap(image, 150, 150);
byte[] byteArr = new byte[file.InputStream.Length];
using (MemoryStream stream = new MemoryStream())
{
resized.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
byteArr = stream.ToArray();
}
file.InputStream.Read(byteArr, 0, byteArr.Length);
profile.ImageUrl = byteArr;
profile.UseGravatar = false;
profileService.UpdateProfile(profile);
this._model.ShowApprovePanel();
}
else
{
_model.ShowMessage("The file you uploaded is larger than the 1mb limit. Please reduce the size of your file and try again.");
}
}
Saving the file physically onto server and than working on the same helped me resolve my issue.

Has Anyone Got HTTP Compression Working with ASP.NET?

I've spent quite a bit of time on this but seem to be going nowhere. I have a large page that I really want to speed up. The obvious place to start seems to be HTTP compression, but I just can't seem to get it to work for me.
After considerable searching, I've tried several variations of the code below. It kind of works, but after refreshing the browser, the results seem to fall apart. They were turning to garbage when the page used caching. If I turn off caching, then the page seems right but I lose my CSS formatting (stored in a separate file) and get an error that an included JS file contains invalid characters.
Most of the resources I've found on the Web were either very old or focused on accessing IIS directly. My page is running on a shared hosting account and I do not have direct access to IIS7, which it's running on.
protected void Application_BeginRequest(object sender, EventArgs e)
{
// Implement HTTP compression
if (Request["HTTP_X_MICROSOFTAJAX"] == null) // Avoid compressing AJAX calls
{
// Retrieve accepted encodings
string encodings = Request.Headers.Get("Accept-Encoding");
if (encodings != null)
{
// Verify support for or gzip (deflate takes preference)
encodings = encodings.ToLower();
if (encodings.Contains("gzip") || encodings == "*")
{
Response.Filter = new GZipStream(Response.Filter,
CompressionMode.Compress);
Response.AppendHeader("Content-Encoding", "gzip");
Response.Cache.VaryByHeaders["Accept-encoding"] = true;
}
else if (encodings.Contains("deflate"))
{
Response.Filter = new DeflateStream(Response.Filter,
CompressionMode.Compress);
Response.AppendHeader("Content-Encoding", "deflate");
Response.Cache.VaryByHeaders["Accept-encoding"] = true;
}
}
}
}
Is anyone having better success with this?
I've had good results using GZipStream and DeflateStream to write the output directly, although I'm not familiar with the Response.Filter property. Give this a whirl:
string response = "your output body";
string accept = Request.Headers["Accept-Encoding"];
if(accept == null) accept = "";
if (response.Length < 100 || !(accept.Contains("deflate") || accept.Contains("gzip")))
Response.Write(response);
else
{
byte[] compressed;
bool useDeflate = accept.Contains("deflate");
using (MemoryStream stream = new MemoryStream())
{
using (Stream deflate = useDeflate
? (Stream)new DeflateStream(stream, CompressionMode.Compress, true)
: (Stream)new GZipStream(stream, CompressionMode.Compress, true))
using (StreamWriter writer = new StreamWriter(deflate))
writer.Write(response);
compressed = new byte[stream.Length];
stream.Position = 0;
stream.Read(compressed, 0, compressed.Length);
}
Response.Headers["Content-Encoding"] = useDeflate ? "deflate" : "gzip";
Response.BinaryWrite(compressed);
}

Resources