Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8288039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:11:50+00:00 2026-06-08T12:11:50+00:00

I have a code snippet which uploads an image. On uploading, it temporarily stores

  • 0

I have a code snippet which uploads an image. On uploading, it temporarily stores the file in a Session. Then on click of “Save”, it saves the file in a database.

There’s no problem on my machine, but on the server, whenever I try to upload some files, and then click on “Save”, it shows me an error “Closed file cannot be accessed”. On Googling it, I read here that this was due to large files being uploaded. I wanted to confirm, is the problem that I’m uploading large files?? Or could it be something else?
Also, why am I not getting this on my machine and only on the server??

EDIT: By the way, the error showed up when the file size > 80kb

Code on Uploading file:

public ActionResult StoreLogoInSession(HttpPostedFileBase file, int assetID)
        {
            string filename = null;
            if (ValidateOnUpload(file))
            {
                Session.Add("TempLogo", file);
                filename = file.FileName;
                Session.Add("filename", filename);
            }
            return RedirectToAction("Edit", new { id = assetID });
        }

Code on Saving (this is when the error occurs):

public ActionResult SaveLogo(LogoModel m, int assetID)
        {
                HttpPostedFileBase file = (HttpPostedFileBase)Session["TempLogo"];
                var logoModel = new LogoModel();
                var asset = this.GenerateAssetForUploadFile(file, (int)m.Asset.AccountID, m.Asset.TextContents, m.Asset.AssetName);
                this.LogoManagementFacade.SaveLogo(asset);
                logoModel.Asset = asset;
                this.LogoModelList.Add(logoModel);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:11:53+00:00Added an answer on June 8, 2026 at 12:11 pm

    The first thing into fixing this issue is to get rid of the Sessions and temporarily store the uploaded file on the file system or if you are running in a web farm on a shared folder or if the files are not large enough you could even store them in the database.

    So let’s suppose that currently you are not running in a web farm and you have a single web server and that we could store the uploaded files in a temporary folder (for which of course you will write a scheduled script that could run every night and delete older than X days files to avoid wasting disk space for nothing):

    public const string AssetsFolderBase = "~/App_Data";
    
    public ActionResult StoreLogo(HttpPostedFileBase file, int assetID)
    {
        string filename = null;
        if (ValidateOnUpload(file))
        {
            var folder = Path.Combine(Server.MapPath(AssetsFolderBase), assetID.ToString());
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            folder = Path.Combine(folder, Path.GetFileName(file.FileName));
            file.SaveAs(folder);
        }
        return RedirectToAction("Edit", new { id = assetID });
    }
    

    and then when you need to access it:

    public ActionResult SaveLogo(LogoModel m, int assetID)
    {
        var folder = Path.Combine(Server.MapPath(AssetsFolderBase), assetID.ToString());
        var file = Directory.GetFiles(folder).FirstOrDefault();
        if (file == null)
        {
            // no file with this assetID was uploaded => throw an exception
            // or return 404 or something
            return HttpNotFound();
        }
    
        // at this stage the file variable is pointing to the uploaded filename
        // => you could process it here ...
    }
    

    Now if you want to handle large file uploads you should obviously make sure to adjust the proper settings in your web.config. By default you are limited to 4MB.

    Oh and while you are editing your web.config make sure you add the following line to ensure that no other developers make the same mistake as you and accidentaly use the ASP.NET Session:

    <sessionState mode="Off" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following code snippet in my ANT File which compiles my project
I have the created a sample Lucene code snippet which indexes a small file.
In my project, I have created a code snippet which can be copied and
I have the following very simple code snippet which is loaded from a separate
i have the following code snippet. in which i just want to return PartyName
I have below snippet of code in which TestClass is extending jPanel which is
I have this code snippet: $(function() { $('.toolbar [id^=button]').on('click', function () { $(this) .css('background-color',
I have a script which uploads files into an online directory and stores the
I have a code snippet which connects to a MySQL database like this (not
I have code snippet, which is like this... #import <sqlite3.h> @interface DatabaseClass : NSObject

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.