简体   繁体   中英

How to upload blob image from html page using javascript to c# code

Currently I have this code right now,

In my html page, on the form I have this:

<input type="file" id="txtUploadFile" accept="image/*" onchange="changetext();"/>

I upload the picture using javascript on my doUpload() function

function doUpload() {
    var srwebserviceURL = "/Webservices/Facilities/ServiceRequest.asmx";
    var sMsgBody = "<filePath>" + txtUploadFile.value + "</filePath>";
    var a = sendSoapMsg(srwebserviceURL, "SaveSRLogoPhotoSite", sMsgBody, "SaveSRLogoPhotoSiteResult");
}

So as you can see from the code above, I am passing the file path of the photo to my webservice.

On my webservice, the SaveSRLogoPhotoSite , I have the ff. code:

public SRLogoPhoto SaveSRLogoPhotoSite(string filePath)
{
    DataSet ds = null;
    Hashtable param = new Hashtable();
    SRLogoPhoto srlp = new SRLogoPhoto();

    try
    {

        System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

        Byte[] b = new Byte[fs.Length];
        fs.Read(b, 0, b.Length);
        fs.Close();
        SqlParameter P = new SqlParameter("@Picture", SqlDbType.VarBinary, b.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, b);

        string sqlStr = "UPDATE SRSiteLogo SET srImage = @Picture ";

        param.Add("Picture", P);

        ds = dbHelper.GetDataSet(sqlStr, param);

    }
    catch (Exception ex)
    {
        srlp.Error = "SaveSRLogoPhotoSite() web method failed on call to dbHelper.GetDataSet - " + ex.Message;
    }

    return srlp;
}

This is working on my local pc. But it does not seem to work when I deploy it to an environment other than my pc. When I try to debug in soapUI, it says that it cannot find the file path .

It seems that the filepath I should pass on my webservice should be on the server first instead of the filepath of the current filesystem of the pc it is in.

How do I do that?

-- edit -- I was informed that this is possible using ajax..I'm new to ajax and don't know how to do it..

Thank you in advance


When you run web on local, file that you need upload and server lie on a machine, so you can determine file path and execute upload task. Howerver, when you deploy your web to another server, you can't determine path file. You have to convert file to stream and send to server, read stream, convert to expect format and go ahead. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM