Upload No Message Pop-up

Jason Phang

Well-known member
Joined
Aug 13, 2019
Messages
46
Programming Experience
Beginner
I am using sweet alert alongside c# asp.net. For my download and delete button, the sweet alert display managed to be displayed when the user clicked the button. For the upload button, i wanted that the code will check if the file uploaded by the user was empty or not. If it was empty, then a message ("No file uploaded") and if it was not empty ('File was uploaded successfully").

Upload Button:
 <div style="width:80%; text-align:left;">
        @using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
        <p>
            <label for="file">Upload file:</label>
            <input type="file" name="file" id="file" />
            <input type= "submit" value="Upload" id= "fileUpload"/>
            <script>
                $('#fileUpload').click(function (e) {
                    if ($('#fileUpload').val() === "") {
                        sweetAlert("No file uploaded");
                }

                 if ($('#fileUpload').val() !== ""){
                    sweetAlert("File Uploaded Succesfully");
                }
              });
            </script>
        </p>
        }

    </div>

However, when i click on the upload button, neither of these pop-up messages are displayed. What am I missing out?
 
First, this is razor stuff, nor does it have anything to do with Visual Studio. So why is it posted here?

Secondly, I'd pay more attention to your js, and validation, as a starting point.

Lastly, I'd implement some proper validation procedures before outputting any messages, otherwise there's nothing stopping someone uploading a malicious file.

Note; also cross-posted here Upload Button No Display Message

Edit out a typo
 
Last edited:
My suggestion: Replace lines 10-16 with a simple alert("Hi!");. If the click event handlers, that should at least show something.

I doubt that it will do something, though. As I recall, the form submit button is special. You need to attach a jQuery event handler to that a different way. Look up the on() jQuery plugin.
 
1571232419866.png

Suprisingly, it managed to displayed that via <script> $('#fileUpload').click(function (e) { alert("tx"); }); </script> . Now I am trying to figure out how to be able to check for if file was empty or not
 
First, this is razor stuff, nor does it have anything to do with Visual Studio. So why is it posted here?

Secondly, I'd pay more attention to your js, and validation, as a starting point.

Lastly, I'd implement some proper validation procedures before outputting any messages, otherwise there's nothing stopping someone uploading a malicious file.

Note; also cross-posted here Upload Button No Display Message

Edit out a typo

Yeah, coz im also that user. Where should i post for razor questions then? For the validation part, as this program is just for my personal mini project, I want to just see whether I am able to display a message box or not.
 
Last edited:
As of now, the upload button will display the message but it would not be able to still check whether there is a file uploaded or not. if i just clicked the upload button, the message will appear.

<script> $('#fileUpload').click(function (e) { if ($('#fileUpload').val() === "") { Swal.fire({ title: 'Great!', text: 'File uploaded', type: 'success', confirmButtonText: 'Nice' }) } else { Swal.fire({ title: 'Wait awhile...', text: 'File will be uploaded shortly', type: 'success', confirmButtonText: 'Okay, cool' }) } }); </script>

I am unsure with my condition for checking if there is a file selected or not.
 
C#:
 <script>
                    $('#fileUpload').click(function (e) {
                        if ($('#fileUpload').val() === "Upload") {
                            Swal.fire({
                                title: 'Wait awhile...',
                                text: 'File will be uploaded shortly',
                                type: 'success',
                                confirmButtonText: 'Okay, cool'
                            })                     
                        }
                        if ($('fileUpload').val() !== "Upload") {
                            Swal.fire({
                                title: 'Nothing',
                                text: 'No file selected',
                                type: 'error',
                                confirmButtonText: 'Again!!'
                            })
                        }
                      
                    });
                </script>

Via this code block, only the second message will be displayed when i select the upload button. The first message that should be displayed when a file is selected is not displayed.
 
Yeah, coz im also that user.
I'm sorry am I sensing some sarcasm? You must think we are stupid if you think we think otherwise, do you? It's considered bad manners, to cross-post, and only makes yourself look like a help vampire.

Do you really think there is someone in this planet, sitting at home writing the exact same question as you word for word, as well as posting exactly the same code as you only to troubleshoot the exact same problem as you?

Maybe the user on SO is a time traveller instead, who travelled through time to make these set of circumstances to entwine these two moments of time together to cause this very elaborate conspiracy? ?
Where should i post for razor questions then?
Well, I'd imagine ASP.NET would at least be more appropriate than Visual Studio.... A mod has already moved your topic for you to the correct forum.

if ($('#fileUpload').val() === "Upload")

Maybe you should pay attention to that stack overflow I linked. And why are you putting them in separate if statements? Didn't you ever here of else and else if.
 
No, its not sarcasm and I understand everyones time here is very valuable. Its just that I am want to complete this project as fast as possible and the reason why i used another account in SO is because I could not post anymore questions in SO as some of my questions were considered as noob. I just wanted to see the options of answers that I have because truthfully speaking, from the resources in the Internet, certain solutions work and for me, most of the time, it does not. Which is why i thought of seeking as much help as possible but i understand that its impolite. Sorry, i will not repeat again.
 
Back
Top Bottom