web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested answer

Throwing error exception and file not getting downloaded

(1) ShareShare
ReportReport
Posted on by
Hi team,
 
I have written a batch job class to download a excel file. However, it throws an exception given below:

 
This is the piece of code, I am using to download the file.

And it generates a download url but when it goes to br.navigate method it trows and error pasted above.

below is the call stack.
 
Can you please guide. Also I noticed that memorystream shows ReadTimeout = 'memoryStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'.
I have the same question (0)
  • Martin Dráb Profile Picture
    236,172 Most Valuable Professional on at
    Throwing error exception and file not getting downloaded
    The description of your problem doesn't make sense to me. You seem to be saying that new SysDictClass(className2Id('mystrategyclass')) creates an instance of FileUploadTemporaryStorageStrategy class instead of mystrategyclass, which is hard to believe. It's more likely that you have a problem in your code or your testing than that SysDictClass went crazy.
     
    Please debug it once more and than give us more concrete information about the behavior.
  • Karuna Dogra Profile Picture
    on at
    Throwing error exception and file not getting downloaded
    public static FileUploadTemporaryStorageResult SendFileToTempStore_GetResult(System.IO.Stream stream, str fileName, classname fileUploadStrategyClassName = classstr(FileUploadTemporaryStorageStrategy), boolean _downloadOnly = false)
        {
            SysDictClass uploadStrategyDicClass;
            FileUploadTemporaryStorageStrategy fileUploadStrategy;
            FileUploadTemporaryStorageResult fileUploadResult = null;
    
            try
            {
                uploadStrategyDicClass = new SysDictClass(className2Id(fileUploadStrategyClassName));
                fileUploadStrategy = uploadStrategyDicClass.makeObject() as FileUploadTemporaryStorageStrategy;
    
                if (fileUploadStrategy == null)
                {
                    warning("@ApplicationPlatform:FileUploadFailedInStrategy");
                    throw Exception::Error;
                }
    
                if (stream == null)
                {
                    throw error(Error::missingParameter(null));
                }
    
                fileUploadResult = fileUploadStrategy.uploadFile(stream, fileName);
    
                if (fileUploadResult == null || !fileUploadResult.getUploadStatus())
                {
                    warning("@ApplicationPlatform:FileUploadFailed");
                }
            }
            catch(Exception::Error)
            {
                warning("@ApplicationPlatform:FileUploadFailed");
            }
            return fileUploadResult;
        }
    If I create my own strategy class and pass that class as strategy class like below, then also when it will got into this sendfiletotempstore method, it will find the same standard strategy as shown in above code. How can I overcome that?
     
    ClassName fileUploadStrategyClassName = classstr(mystrategyclass);
                downloadUrl = File::SendFileToTempStore(memoryStream, fileName, fileUploadStrategyClassName, true);
  • Martin Dráb Profile Picture
    236,172 Most Valuable Professional on at
    Throwing error exception and file not getting downloaded
    If you extended the class, you would change the timeout everywhere, not in your scenario. That's why I explicitly talked about a custom strategy. What time you should use depends on your requirements. I don't know them.
  • Karuna Dogra Profile Picture
    on at
    Throwing error exception and file not getting downloaded
    Thanks a ton Martin,
     
    Using xlsx extension in name, solved the problem.
    Now I am working on increasing timeout as its 10 mins by default.
     
    As mentioned by your earlier- pay attention to the third parameter, fileUploadStrategyClassName. The default is FileUploadTemporaryStorageStrategy, which has FileTimeOutInMinutes = 10. You may want to create a custom strategy with a longer time.
     
    1. Should I create a new strategy class (copy of this class with increased time) or can I create extension of this class and then increase timing in my extension.
     
    I see there are these two methods related to time span.
       /// <summary>
        /// The default file link timeout in minutes
        /// </summary>
        public const System.Double FileTimeOutInMinutes = 10;
    
       public System.Double getBlobLinkExpirationTimeSpanInMinutes()
        {
            System.Double expirationOverride = FileUploadConfiguration::find().File_BlobLinkExpirationTimeSpanInMinutes;
            
            System.Double minutes = FileTimeOutInMinutes;
    
            if (expirationOverride > 0)
            {
                minutes = expirationOverride;
            }
    
            return minutes;
        }
    2. For how long it can be increased?
  • Martin Dráb Profile Picture
    236,172 Most Valuable Professional on at
    Throwing error exception and file not getting downloaded
    What is "a file type file"? Excel files, text files, zip files, XML files... all are files.
     
    Before, you claimed you're getting a zip file. A .xlsx file is a zip file, therefore you're likely getting the right thing. Change the name to something with the .xslx extension and try to open in it in Excel. If it works, the answer to my question in the previous thread is "yes, your only problem is with the file name".
  • Karuna Dogra Profile Picture
    on at
    Throwing error exception and file not getting downloaded
    Below is the code I have written for downloading the file.
    [Form]
    public class xyzNAPExcelDownLoadForm extends FormRun
    {
        [Control("CommandButton")]
        class DownloadExcel
        {
            /// <summary>
            /// ]
            /// </summary>
            public void clicked()
            {
                        
                
                new Browser().navigate(xyzNAPGlobalProdQtyExcelURL_DownloadURL.valueStr());
                super();
            }
    
        }
    
    }

    Approach I am using -: I am now using the temporary location to save the file using SendFileToTempStore(). Below is the code for that
    str  fileName  = strFmt('GlobalProductionQty-%1-%2', starttime, DateTimeUtil::utcNow());
            
            if(this.isExecutingInBatch())
            {
                str downloadUrl;
                ClassName fileUploadStrategyClassName = classstr(FileUploadTemporaryStorageStrategy);
                downloadUrl = File::SendFileToTempStore(memoryStream, fileName, fileUploadStrategyClassName, true);
    
                if (downloadUrl)
                {
                    globalProdQtyExcelURL.DownloadURL       = downloadUrl;
                    globalProdQtyExcelURL.BatchJobDateTime  = DateTimeUtil::utcNow();
                    globalProdQtyExcelURL.insert();
                }
            }
    through above code, I am generating the download url and store it in a table and presenting it to user through a simple list form given below: So, on this form when user click on download excel then it should generate the file in .xslx format but its generating a file type file.
     
    Hence problem is : my file is not getting downloaded in .xslx format. How can I achieve this.
     
    Also, just informing - I have written below code, when user is not running the process in batch and it generates the xlsx format file.
     
      //Download the file.
                DocuFileSaveResult          saveResult      = DocuFileSaveResult::construct();
                saveResult.parmAction(DocuFileSaveAction::Download);
                saveResult.parmFilename(strFmt('%1.xlsx', fileName));
                DocuFileSave::processSaveResult(memoryStream,saveResult);
           
     
  • Martin Dráb Profile Picture
    236,172 Most Valuable Professional on at
    Throwing error exception and file not getting downloaded
    Regarding your last problem, please show us your code for the download based on your table.
     
    Do I understand correctly that the only problem is that the correct extension isn't offered to the user when downloading but everything works if the user saves the file with .xslx extension? Please confirm; your problem description wasn't detailed enough.
  • Martin Dráb Profile Picture
    236,172 Most Valuable Professional on at
    Throwing error exception and file not getting downloaded
    You keep asking about things that I already answered. No, element.args().record() isn't code for getting a value of a data source of the current form. It gets a record that the form was called with from another place. The usual approach for getting the selected record from a data source is using the automatically created variable with the same name as the data source (e.g. myTable).
  • Karuna Dogra Profile Picture
    on at
    Throwing error exception and file not getting downloaded
    Hi,
     
    Now, I am able to get the download url and store it in my table. I have created a form where user will select the download url grid  and click on download excel button to download the file. But there is one problem now.
     
    memoryStream.Seek(0,System.IO.SeekOrigin::Begin); 
    str  fileName  = strFmt('GlobalProductionQty-%1-%2', starttime, DateTimeUtil::utcNow());
            
            if(this.isExecutingInBatch())
            {
                str downloadUrl;
                ClassName fileUploadStrategyClassName = classstr(FileUploadTemporaryStorageStrategy);
                downloadUrl = File::SendFileToTempStore(memoryStream, fileName, fileUploadStrategyClassName, true);
    
                if (downloadUrl)
                {
                    globalProdQtyExcelURL.DownloadURL       = downloadUrl;
                    globalProdQtyExcelURL.BatchJobDateTime  = DateTimeUtil::utcNow();
                    globalProdQtyExcelURL.insert();
                }
            }
            //end
           
            else
            {
                //Download the file.
                DocuFileSaveResult          saveResult      = DocuFileSaveResult::construct();
                saveResult.parmAction(DocuFileSaveAction::Download);
                saveResult.parmFilename(strFmt('%1.xlsx', fileName));
                DocuFileSave::processSaveResult(memoryStream,saveResult);
            }
    I am using above code, when running in batch the download url generated is not giving the excel file. Instead a 'file' type folder is getting downloaded and when thats open in browser, it downloads a zip folder and then there are below files in that folder but prepared excel is not there. However when not run in batch, my excel is downloaded prperly. Please help how can I get excel from the download url instead of folder.
     
  • Karuna Dogra Profile Picture
    on at
    Throwing error exception and file not getting downloaded
    Yes thats right, I want to use data source of the form, not a record passed from another form when opening this form.
     
    On my current form, I will select the grid record and then when I click on the button download excel, I want to get the current datasource's fields value(excel download url).
     
    Is this statement right for that?
     
    xyzGlobalProdQtyExcelURL  urlTable = element.args().record();

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 2,352

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 830 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 634 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans