Answered Need help with uploading large files through ASP Web Service

  • Thursday, April 12, 2012 9:09 PM
     
     

    I am uploading large files through ASP Web service. I tried running the program from my local server IIS and it works fine but, when I host it in a third party IIS server and try uploading around  25MB file I get this following error:

    Unhandled Exception: System.Net.WebException: The request failed with HTTP statu
    s 404: Not Found.
    at System.Web.Services.Protocols.SoapHttpCl…
    ntMessage message, WebResponse response, Stream responseStream, Boolean asyncCal
    l)
    at System.Web.Services.Protocols.SoapHttpCl… methodN
    ame, Object[] parameters)
    at ConsoleApplication1.famous.Service.uploa… uname, String pass, S
    tring UID, Byte[] f, String fileName, Int32 fTypeNum, String title, String descr
    iption, Int32 clicks, Int32 fileSize, Boolean ageRated, Int32 nViews, String gen
    re) in C:\Users\Rajkumar\Documents\Visual Studio 2010\Projects\ConsoleApplicatio
    n1\Web References\famous\Reference.cs:line 304
    at ConsoleApplication1.Program.Main(String[… args) in C:\Users\Rajkumar\Docum
    ents\Visual Studio 2010\Projects\ConsoleApplication1\Progra… 36

    WebMethod I used is:

    [WebMethod]

    public String securedUploadFile(String uname, String pass, String UID, byte[] f, String fileName, int fTypeNum, String title, String description, int clicks, int fileSize, bool ageRated, int nViews, String genre)
            {
                try
                {
                    String fileType = String.Empty;
                    String fileExtension = String.Empty;
                    switch (fTypeNum)
                    {
                        case 1: fileType = "Image";
                            fileExtension = ".jpg";
                            break;
                        case 2: fileType = "Music";
                            fileExtension = ".mp3";
                            break;
                        case 3: fileType = "Video";
                            fileExtension = ".swf";
                            break;
                        default: fileType = "1002";
                            break;
                    }

                    if (fileType != "1002")
                    {

                        if (getUserID(uname, pass).Equals(UID))
                        {

                            connection = establishConnection(connection);
                            cmd = new MySqlCommand();
                            cmd.Connection = connection;
                            if(fileName.Contains("."))
                            fileName = fileName.Substring(0, fileName.IndexOf("."));
                            switch (fTypeNum)
                            {
                                case 1: cmd.CommandText = "select count(fileName) from ImageUploads where fileName like '" + fileName + "%';";
                                    break;

                                case 2: cmd.CommandText = "select count(fileName) from AudioUploads where fileName like '" + fileName + "%';";
                                    break;

                                case 3: cmd.CommandText = "select count(fileName) from VideoUploads where fileName like '" + fileName + "%';";
                                    break;

                            }

                            int nFileExistence = Convert.ToInt32(cmd.ExecuteScalar().ToString());


                           //   String path = @"C:\inetpub\wwwroot\SampleService\UserFiles\" + UID + @"\" + fileType + @"\";
                            String path = "D:\\Hosting\\9176285\\html\\WebService\\UserFiles\\" + UID;
                            path = path + "\\" + fileType + "\\";
                            //if (!Directory.Exists(path))
                            Directory.CreateDirectory(path);
                            String virtualPath = "UserFiles/" + UID + "/" + fileType;
                            nFileExistence++;
                            fileName = fileName + "(" + nFileExistence + ")";
                       //     Console.WriteLine(fileName);
                        //    FileStream fs = new FileStream(HostingEnvironment.MapPath
                         //              ("~/" + virtualPath + "/") + fileName + fileExtension, FileMode.Create, FileAccess.ReadWrite, FileShare.None);

                            Random uploadID = new Random();
                            String uID;
                            while (true)
                            {
                                uID = uploadID.Next(100000, 999999).ToString();
                                cmd.CommandText = "call checkUploadIDExistence('" + uID + "')";
                                if (Convert.ToInt32(cmd.ExecuteScalar().ToString()) != 1000)
                                    break;
                            }

                            cmd.CommandText = "insert into Uploads values('" + uID + "', '" + UID + "', '" + title + "', DATE(now()), now(), '" + description + "', " + clicks + ", " + fileSize + ", " + ageRated + ", " + nViews + ");";
                            int ab = cmd.ExecuteNonQuery();
                            String fileTypeID;
                            while (true)
                            {
                                fileTypeID = uploadID.Next(100000, 999999).ToString();
                                switch (fTypeNum)
                                {
                                    case 1: cmd.CommandText = "call checkImageIDExistence('" + fileTypeID + "')";
                                        break;
                                    case 2: cmd.CommandText = "call checkMusicIDExistence('" + fileTypeID + "')";
                                        break;
                                    case 3: cmd.CommandText = "call checkvideoIDExistence('" + fileTypeID + "')";
                                        break;
                                }

                                if (Convert.ToInt32(cmd.ExecuteScalar().ToString()) != 1000)
                                    break;
                            }
                            path = path.Replace(@"\", "/");
                            switch (fTypeNum)
                            {
                                case 1: cmd.CommandText = "insert into ImageUploads values('" + fileTypeID + "', '" + uID + "', '" + path + "', '" + fileName + "');";
                                       break;
                                case 2: cmd.CommandText = "insert into AudioUploads values('" + fileTypeID + "', '" + uID + "', '" + genre + "', '" + path + "', '" + fileName + "');";
                                    break;
                                case 3: cmd.CommandText = "insert into VideoUploads values('" + fileTypeID + "', '" + uID + "', '" + path + "', '" + fileName + "');";
                                    break;
                            }
                            int bc = cmd.ExecuteNonQuery();

                            if (ab == 1 && bc == 1)
                            {
                                path = path.Replace("/", @"\");
                              ///  System.Security.AccessControl.FileSecurity fSecurity = new System.Security.AccessControl.FileSecurity();
                              // System.Security.AccessControl.FileSystemAccessRule fAccess = new System.Security.AccessControl.FileSystemAccessRule("oceansfive", System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow);
                             //   fSecurity.AddAccessRule(fAccess);
                             //   File.SetAccessControl(path, fSecurity);
                                File.WriteAllBytes(path + fileName + fileExtension, f);
                              //  fs.Write(f, 0, f.Length);
                              //  fs.Close();
                                return "File Uploaded Successfully !";
                            }
                            else
                            {
                                return "File Upload Failed !";
                            }
                        }
                        else
                        {
                            return "You are not authorised for this action !";
                        }
                    }
                    else
                    {
                        return "Invalid file type";
                    }

                }

                catch (Exception ex)
                {
                    return ex.Message.ToString();
                }

            }


    Web.config is

    <?xml version="1.0"?>
    <!--
        Note: As an alternative to hand editing this file you can use the
        web admin tool to configure settings for your application. Use
        the Website->Asp.Net Configuration option in Visual Studio.
        A full list of settings and comments can be found in
        machine.config.comments usually located in
        \Windows\Microsoft.Net\Framework\vx.x\Config
    -->
    <configuration>
        <appSettings/>
        <connectionStrings/>
        <system.web>
            <!--
                Set compilation debug="true" to insert debugging
                symbols into the compiled page. Because this
                affects performance, set this value to true only
                during development.
            -->
        <httpRuntime executionTimeout="700000" maxRequestLength="6144000" />
        
        <customErrors mode="Off" />
            <compilation debug="true" targetFramework="4.0">
                <assemblies>
                    <add assembly="MySql.Data, Version=6.4.4.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
                    <add assembly="MySql.Data.Entity, Version=6.4.4.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
                </assemblies></compilation>
            <!--
              The <authentication> section enables configuration
              of the security authentication mode used by
              ASP.NET to identify an incoming user.
            -->
            <authentication mode="Windows"/>
            <!--
               The <customErrors> section enables configuration
               of what to do if/when an unhandled error occurs
               during the execution of a request. Specifically,
               it enables developers to configure html error pages
               to be displayed in place of a error stack trace.

               <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
                 <error statusCode="403" redirect="NoAccess.htm" />
                 <error statusCode="404" redirect="FileNotFound.htm" />
               </customErrors>
            -->
            <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>
        <!--
            The system.webServer section is required for running ASP.NET AJAX under Internet
            Information Services 7.0.  It is not necessary for previous version of IIS.
        -->
    </configuration>







    • Edited by C.M.R Thursday, April 12, 2012 9:19 PM Pasted the web.config and the web method used for file upload
    • Edited by C.M.R Thursday, April 12, 2012 9:22 PM
    •  

All Replies

  • Thursday, April 12, 2012 10:26 PM
     
      Has Code

    I had this error with a WCF service I am writing.  Although the answer did not seem logical based on the error message, it turned out that the problem had to do with the size of the data I was uploading through the service.  I am not sure where you would place this information in your case, but for me, I had to include a readerQuota setting in the binding tag.  Since I am still writing this service, I have not optimized this information.  I know that any file this large would probably timeout, but this is the line in my config file that allowed my service to function:

    <readerQuotas maxStringContentLength="2147483647" maxArrayLength="163840000" />
    Hope that helps!

    Christine A. Piffat

  • Friday, April 13, 2012 12:20 AM
     
     

    Hi, I don't have a clue where to place this element because, intellisense feature couldn't recognise readerQuotas element unfortunately. I tried adding few lines:

    <sessionState mode="StateServer" timeout="20"/>

    and

    this.Session.Timeout = 20;

    These doesn't seems to work as well


    nice guy

  • Friday, April 13, 2012 6:20 AM
     
      Has Code

    Make sure your asmx web service accessible.

    Make sure you have correct address configured on configuration file of asp.net’s web.config.

    For large data, asmx web service provides two additional parameters you can configure

    • maxMessageLength
    • executionTimeoutInSeconds
    <configuration>
      <system.web>
      <httpRuntime maxMessageLength="409600"
        executionTimeoutInSeconds="300"/>
      </system.web>
    </configuration>

    Refer http://msdn.microsoft.com/en-us/library/aa529330.aspx to configure this.

    This forum is for WCF, You can post or move your question to ASMX Web Services and XML Serialization Forum where some expert can assist you.


    Lingaraj Mishra

  • Friday, April 13, 2012 6:37 AM
    Moderator
     
     

    The HTTP status 404 illustrates that the server has not found anything matching the URI given. you can set a breakpoint in the web method and debug it (in the debug menu, 'attach to process' to aspnet_wp.exe or w2wp.exe). if you want to upload large file using web service, you need to set the maxRequestLength and executionTimeout in httpRuntime appropriately and increase their values. Here is a article about uploading large files using web service.

    http://www.codeproject.com/Articles/43272/Uploading-Large-Files-Through-Web-Service


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework

  • Friday, April 13, 2012 10:29 AM
     
     
    The link which you have provided helps to uploadLarge files using FileStream which normally wouldn't support uploading large files. It could be done by splitting the bytes into chunks as how he has done but, on my case I have used File class which doesn't require such things. File class by default writes large files even if it is 50 MB. It works in my home but, it doesn't work only when I host it on the server.

    nice guy


    • Edited by C.M.R Friday, April 13, 2012 10:41 AM
    •  
  • Friday, April 13, 2012 10:38 AM
     
     
    Web Service is accessible and I am able to consume other services through that. The only problem which I am facing is this upload functionality. I tried my level to get this work but, I couldn't succeed in this. I'm able to upload 50MB files on my local IIS server but, I have hosted my service in godaddy hosting service and I couldn't upload large files only there. I believe it is the time out which is causing that issue because, I tried checking the time taken to reject the request and all the requests get rejected at the same time.

    nice guy

  • Monday, April 16, 2012 8:38 AM
    Moderator
     
     Answered

    First you can increasing the value of maxRequestLength and executionTimeoutInSeconds in httpRuntime as mentioned above, and here is an additional item that can be added to the web service's web.config file to permit large file transfers.

    C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI


     <location path="Copy.asmx"> <!-- Name of you asmx -->
         <system.webServer>
           <security>
             <requestFiltering>
               <requestLimits maxAllowedContentLength="104857600"/> <!-- 100 megs -->
             </requestFiltering>
           </security>
         </system.webServer>
       </location>

    This appears to enable larger files to be uploaded via web services, For more about this, please check this similar post.

    http://stackoverflow.com/questions/998927/how-do-i-upload-large-25mb-files-to-a-web-service


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework