4

I am trying to upload some files wtih different extensions like .png,.jpg,.txt,.pdf etc. to IIS server .However my all files are created to given path but they are all 1kb size and when you open them it says

The original file is in the temp folder. Full path of the file: C:\Temp\xxxxxxxxx.tmp

My code works perfectly on localhost when running from the source code and There is no such a folder on web server's directory like mentioned in the message.

My code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUploadTest.aspx.cs" Inherits="FileUploadTest" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
                <asp:FileUpload ID="FileUpload1"  Multiple="Multiple" name="okantestfileupload" runat="server" />
                <asp:Button ID="btnFileUpload" OnClick="btnFileUpload_Clickk" runat="server" Text="ekle" Width="150px" CssClass="CommandButton" />
               <asp:ListBox runat="server" ID="ListBoxForInfo" Width="400" CssClass="NormaltextBox" ></asp:ListBox>
        </div>
    </form>
</body>
</html>

.cs part

protected void btnFileUpload_Clickk(object sender, EventArgs e)
    {
        string strFolder = @"D:\TEST\";
        HttpFileCollection uploadedFiles = Request.Files;

        for (int i = 0; i < uploadedFiles.Count; i++)
        {
            HttpPostedFile uploadedFile = Request.Files.Get(i);


            var uFileSize = uploadedFile.ContentLength;
            var uFileName = uploadedFile.FileName;
            var uContentType = uploadedFile.ContentType;
            string uExtension =
               System.IO.Path.GetExtension(uploadedFile.FileName);

            uploadedFile.SaveAs(strFolder + uFileName);
            ListBoxForInfo.Items.Insert(0, uFileName + "." + uContentType + " Boyut: (" + uFileSize + " bytes)  yüklendi.");


        }
    }

What should I do to fix that?I have already tried some solutions including taking consideration of antivirus programmes.

Regards

  • Have you provided write permission to IISUSR for the folder?? – Gagan Deep Feb 22 '19 at 11:46
  • Why are you using `Request.Files` rather than `FileUpload1`? Does the technique shown in https://stackoverflow.com/a/32731146/34092 or https://asp.net-tutorials.com/controls/file-upload-control/ work? – mjwills Feb 22 '19 at 11:48
  • Sorry wrong edit:) Actually I have read all the tutorials and the code works on my local .I am now checking the write permissions on IISUSR but it has a less probability that causes this situation.Because folders are created on the path given on IIS server – SophisticatedUndoing Feb 22 '19 at 11:52
  • If you use `System.IO.File` to write a text file to `D:\Test\bob.txt` does it succeed? – mjwills Feb 22 '19 at 12:06
  • @mjwills yes it has been written interestingly – SophisticatedUndoing Feb 22 '19 at 12:40
  • When you debug the code, what is the **exact** value of `strFolder + uFileName`? `uFileName`? – mjwills Feb 22 '19 at 13:09
  • the exact value is D:\TEST\file.png (extension is irrelevant at this because all of the files' content become like the statement I mentioned in the question) – SophisticatedUndoing Feb 25 '19 at 06:06
  • What happens if you use `strFolder = Server.MapPath("");`? If the files are stored correctly then you know you do not have permission to write outside the root folder of the website. – VDWWD Feb 25 '19 at 09:27
  • I have all permissions to users on IIS server even on Everyone user.The files are created very well but contents are corrupted. – SophisticatedUndoing Feb 27 '19 at 05:25

3 Answers3

3

The problem was the Managed Pipeline Mode configuration of the related Application Pool.The application was run on v2.0 .NET CLR Version but the project was built on .NET 3.5. So changing the Managed PipeLine Mode from Classic to Integrated solved the problem unintentionally without knowing the detailed mechanism. Probable incompatibilities may occur at other external components used in the project.The full cycle test is being performed right now but the upload problem has been fixed.

  • Good that you were able to solve your issue by yourself; hope you are taking the learning back to check for all relavent configurations and setting before hitting somewhere else. Happy coding, cheers – Abhinaw Kaushik Feb 28 '19 at 12:34
  • 1
    It was kinda trial-and-error situation.I assure you that I tried so many methods before the post.Hope somebody can fix their issue faster when they see this solution. happy coding too – SophisticatedUndoing Feb 28 '19 at 12:43
  • I am getting the same error! Managed PipeLine Mode is already integrated! File upload is not working even in Visual Studio! – Khalid Bin Sarower Oct 12 '22 at 10:58
0
  1. Exclude the folder from project whenever you are building a project.
  2. Clean the project
  3. Build and deploy again
umesh.chape
  • 2,895
  • 5
  • 16
  • 17
0

change this folder line to

string strFolder = @"D:\TEST\"; HttpContext.Current.Server.MapPath("~/test"); and place the file under same directory as Website

If Website cannot access d:\test the file stays in temp folder.

Jin Thakur
  • 2,711
  • 18
  • 15